将数组作为参数传递
本文关键字:参数传递 数组 | 更新日期: 2023-09-27 18:22:06
在以下示例中,初始化字符串数组,并将其作为参数传递给字符串的PrintArray方法。该方法显示数组的元素。接下来,调用方法ChangeArray和ChangeArrayElement来证明按值发送数组参数不会阻止对数组元素的更改。我的问题是数组在方法changeArray中是如何不发生变化的。。但是方法ChangeArrayElements发生了变化?这是一个例子:
class ArrayClass
{
static void PrintArray(string[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
}
System.Console.WriteLine();
}
static void ChangeArray(string[] arr)
{
// The following attempt to reverse the array does not persist when
// the method returns, because arr is a value parameter.
arr = (arr.Reverse()).ToArray();
// The following statement displays Sat as the first element in the array.
System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
}
static void ChangeArrayElements(string[] arr)
{
// The following assignments change the value of individual array
// elements.
arr[0] = "Sat";
arr[1] = "Fri";
arr[2] = "Thu";
// The following statement again displays Sat as the first element
// in the array arr, inside the called method.
System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]);
}
static void Main()
{
// Declare and initialize an array.
string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
// Pass the array as an argument to PrintArray.
PrintArray(weekDays);
// ChangeArray tries to change the array by assigning something new
// to the array in the method.
ChangeArray(weekDays);
// Print the array again, to verify that it has not been changed.
System.Console.WriteLine("Array weekDays after the call to ChangeArray:");
PrintArray(weekDays);
System.Console.WriteLine();
// ChangeArrayElements assigns new values to individual array
// elements.
ChangeArrayElements(weekDays);
// The changes to individual elements persist after the method returns.
// Print the array, to verify that it has been changed.
System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:");
PrintArray(weekDays);
}
}
// Output:
// Sun Mon Tue Wed Thu Fri Sat
// arr[0] is Sat in ChangeArray.
// Array weekDays after the call to ChangeArray:
// Sun Mon Tue Wed Thu Fri Sat
//
// arr[0] is Sat in ChangeArrayElements.
// Array weekDays after the call to ChangeArrayElements:
// Sat Fri Thu Wed Thu Fri Sat
在此方法中:
static void ChangeArray(string[] arr)
{
// The following attempt to reverse the array does not persist when
// the method returns, because arr is a value parameter.
arr = (arr.Reverse()).ToArray();
// The following statement displays Sat as the first element in the array.
System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
}
您没有更改源数组-您正在创建一个新数组,并将引用保存在同一变量中。
由于arr
中的引用是按值传递的,因此调用方仍然有对原始数组的引用-对反向数组有新引用这一事实不会改变调用方的引用。
根据约定,在ChangeArrayElements
中,您正在更改传入的数组,因此调用者可以看到这些更改。
如果数组引用由引用传递:
static void ChangeArray(ref string[] arr)
{
// Since 'arr' is passed by reference, changing the value of 'arr'
// changes the reference that the caller has.
arr = (arr.Reverse()).ToArray();
// The following statement displays Sat as the first element in the array.
System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
}
然后,调用者将看到更改,因为您将更改调用者持有的引用。
这里有一个故事(希望)说明了差异:
按值传递引用类型:
Main
-向universe请求一个数组-universe告诉它"数组在插槽1中"Main
-在标签为"weekDays"的便签上写一个1Main
-告诉ChangeArray
在槽1处存在阵列ChangeArray
——在一张标有arr
的便签上写下数字1ChangeArray
-请求宇宙将数组Reverse
。宇宙反转数组,告诉ChangeArray
新数组在插槽2中ChangeArray
-在标有arr
的便签上划掉1,并写一个2
正如您所看到的,由于Reverse
不会更改原始数组,因此调用者引用的数组不会更改。
通过引用传递引用类型:
Main
-向宇宙请求一个数组-宇宙告诉它"数组在插槽1中"Main
-在标签为"weekDays"的便签上写1Main
-递给ChangeArray
上面有数字1的便签ChangeArray
-请求宇宙Reverse
数组。宇宙反转数组,告诉ChangeArray
新数组在插槽2中ChangeArray
-在便签上划掉1,写一个2
在这种情况下,调用者现在有一个对反向数组的新引用。由于传递的是引用("便签"),而不是值 现在让我们看看 请注意,这里的区别在于原始数组被修改了。由于调用方和函数都在查看同一个数组,所以它们都可以看到更改。ChangeArrayElements
:Main
-标签为"weekDays"的便签上仍有1Main
-告诉ChangeArrayElements
在槽1处存在阵列ChangeArrayElements
——在一张标有arr
的便签上写下数字1ChangeArrayElements
—插槽1中阵列的前三个存储桶中的值的变化