The ref returns and locals

Since C# 1.0, the language has supported passing parameters to a method by reference using the ref, but there exists no mechanism to return a safe reference to stack or heap memory locations.

In C# 7.0, Microsoft has provided the option for developers to return values by reference and store them in local variables as a reference pointer.

Before going into an example of return by reference, let us first look at an example of how the return by value works with a pass by reference parameter. In the following example, the GetAsValue method accepts a third parameter of type integer as a reference, and returns a value to the callee, which gets stored in the local variable name:

    public static void DemoReturnAsValue() 
    { 
      var count = 0; 
      var index = 0; 
      string[] names = { "Kunal", "Manika", "Dwijen" }; 
      string name = GetAsValue(names, index, ref count); 
 
      Console.WriteLine("No. of strings in the array: " + count); 
      Console.WriteLine("Name at {0}th index is: {1}
", index, name); 
      name = "Rajat"; 
      Console.WriteLine("The value of 'name' variable changed to:" + name); 
      Console.WriteLine("The new name at {0}th index is still: {1}
",
index, GetAsValue(names, index, ref count)); } public static string GetAsValue(string[] names, int index,
ref int count) { count = names.Length; return names[index]; }

Now, when we change the value of the variable name, it does not change the original string of the array, as it returned by value and there's no reference between them. If you run the preceding example, you will see the following output, where the zeroth position of the array still has the previous/original value. The count variable that we passed as reference parameter will have the length of the array as it is marked as ref:

In C# 7.0, if you want to return something from a method by reference, you should mark the return type with the ref keyword and put it as a reference in a local variable. This is how the preceding implementation of the GetAsValue will change, along with the implementation of storing it:

Now, when you change the value of the variable name, it will change the original value of the array as we are referencing its memory address. Printing the value of the array will give you the changed output. Here is the complete code implementation for the preceding changes, which should give you a clear understanding:

    public static void DemoReturnAsReference() 
    { 
      var count = 0; 
      var index = 0; 
      string[] names = { "Kunal", "Manika", "Dwijen" }; 
      ref string name = ref GetAsReference(names, index, ref count); 
 
      Console.WriteLine("No. of strings in the array: " + count); 
      Console.WriteLine("Name at {0}th index is: {1}
", index, name); 
 
      name = "Rajat"; 
      Console.WriteLine("The value of 'name' variable changed to: " + name); 
      Console.WriteLine("The new name at {0}th index is now: {1}
", index, 
GetAsReference(names, index, ref count)); } public static ref string GetAsReference(string[] names,
int index, ref int count) { count = names.Length; return ref names[index]; }

When you run the code, you will get the preceding output, where the zeroth index of the array now has the new name, which we have changed by reference.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.150.163