7

我阅读了使用 ref 和 out 传递数组页面(C# 编程指南),并想知道为什么我们需要将数组参数定义为 ref 参数,而它已经是引用类型。被调用函数的变化不会反映在调用函数中吗?

4

2 回答 2

28

被调用函数的变化不会反映在调用函数中吗?

对数组内容的更改将反映在调用方方法中 - 但对参数本身的更改不会。例如:

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

现在,如果我们更改Foo为具有以下签名:

public void Foo(ref int[] x)

并将调用代码更改为:

Foo(ref original);

然后它将打印 20。

理解变量和它的值所指的对象之间的区别是非常重要的——同样,修改对象和修改变量之间的区别也是如此。

有关更多信息,请参阅我关于 C# 中的参数传递的文章

于 2013-10-08T19:58:22.673 回答
5

如果您只打算更改数组的内容,那么您是正确的。但是,如果您打算更改数组本身,则必须通过引用传递。

例如:

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.
}
于 2013-10-08T20:00:45.507 回答