
C#中数组作为参数传递的问题,非常疑惑,恳请各位大神指点,帮忙看下这个MSDN上的例程,谢谢了! 30
一个MSDN上C#中数组作为参数传递的例程:classArrayClass{staticvoidPrintArray(string[]arr){for(inti=0;i<...
一个MSDN上C#中数组作为参数传递的例程:
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);
}
}
程序的输出是:
// 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
这就奇怪了,两个函数ChangeArray()和ChangeArrayElements()都是传递的同一个数组作为参数,也都是在各自的函数内部对传递进来的数组参数作更改,但为什么ChangeArray()没能改变weekDays数组的值,而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);
}
}
程序的输出是:
// 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
这就奇怪了,两个函数ChangeArray()和ChangeArrayElements()都是传递的同一个数组作为参数,也都是在各自的函数内部对传递进来的数组参数作更改,但为什么ChangeArray()没能改变weekDays数组的值,而ChangeArrayElements()却改变了呢?实在不得其解,恳求指点!谢谢! 展开
2个回答
展开全部
C语言学过吧, ChangeArray函数中的arr变量就相当于C里的调用函数里的动态变量,当函数结束时就释放了,而ChangeArrayElements函数是对数组元素(string型)赋值,所以改了。回答的不好别笑啊,这种事有时候只可意会不可言传,麻烦你把上面这些代码的链接发给我,前段时间看到过,现在找不到了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-03-22
展开全部
ChangeArrayElements 是改变了某个元素的值。
ChangeArray 只是反转了,改变了元素的顺序
ChangeArray 只是反转了,改变了元素的顺序
更多追问追答
追问
是这样没错啊,但请问,ChangeArrayElements()和ChangeArray()都有对参数数组赋值的语句
:
arr[0] = "Sat";
和
arr = (arr.Reverse()).ToArray();
这不是一回事吗?都是对参数数组进行操作啊,为什么结果不一样呢?感谢!
追答
ChangeArray() 方法中
这句不报错吗??
arr = (arr.Reverse()).ToArray();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询