Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would ref be used for array parameters in C#?

Tags:

c#

out

ref

I read the page Passing Arrays Using ref and out (C# Programming Guide) and was wondering why we would need to define an array parameter as a ref parameter when it is already a reference type. Won't changes in the callee function be reflected in the caller function?

like image 218
Tyler Durden Avatar asked Oct 08 '13 19:10

Tyler Durden


People also ask

Why are arrays passed by reference in C?

The only reason for passing an array explicitly by reference is so that you can change the pointer to point to a different array. If a function only looks at the contents of an array, and does not change what is in the array, you usually indicates that by adding const to the parameter.

Can array be passed by reference?

Like all Java objects, arrays are passed by value ... but the value is the reference to the array. Real passing by reference involves passing the address of a variable so that the variable can be updated. This is NOT what happens when you pass an array in Java.

What is the difference between a ref parameter and out parameter?

ref is used to state that the parameter passed may be modified by the method. in is used to state that the parameter passed cannot be modified by the method. out is used to state that the parameter passed must be modified by the method.

How do you reference an array in a function in C?

Passing arrays to functions in C/C++ are passed by reference. Even though we do not create a reference variable, the compiler passes the pointer to the array, making the original array available for the called function's use. Thus, if the function modifies the array, it will be reflected back to the original array.


2 Answers

Won't changes in the callee function be reflected in the caller function?

Changes to the contents of the array would be reflected in the caller method - but changes to the parameter itself wouldn't be. So for example:

public void Foo(int[] x)
{
    // The effect of this line is visible to the caller
    x[0] = 10;

    // This line is pointless
    x = new int[] { 20 };
}
...
int[] original = new int[10];
Foo(original);
Console.WriteLine(original[0]); // Prints 10

Now if we changed Foo to have a signature of:

public void Foo(ref int[] x)

and changed the calling code to:

Foo(ref original);

then it would print 20.

It's very important to understand the difference between a variable and the object that its value refers to - and likewise between modifying an object and modifying a variable.

See my article on parameter passing in C# for more information.

like image 67
Jon Skeet Avatar answered Sep 29 '22 04:09

Jon Skeet


If you only plan to change the contents of the array, then you're correct. However, if you plan on changing the array itself, then you must pass by reference.

For example:

void foo(int[] array)
{
  array[0] = 5;
}

void bar(int[] array)
{
  array = new int[5];
  array[0] = 6;
}

void barWithRef(ref int[] array)
{
  array = new int[6];
  array[0] = 6;
}


void Main()
{
  int[] array = int[5];
  array[0] = 1;

  // First, lets call the foo() function.
  // This does exactly as you would expect... it will
  // change the first element to 5.
  foo(array);

  Console.WriteLine(array[0]); // yields 5

  // Now lets call the bar() function.
  // This will change the array in bar(), but not here.
  bar(array);

  Console.WriteLine(array[0]); // yields 1.  The array we have here was never changed.

  // Finally, lets use the ref keyword.
  barWithRef(ref array);

  Console.WriteLine(array[0]); // yields 5.  And the array's length is now 6.
}
like image 25
poy Avatar answered Sep 29 '22 04:09

poy