【发布时间】:2010-12-14 22:26:55
【问题描述】:
当引用变量可以通过引用传递时:
class Example
{
public string str="Demo";
public int[] intValues={1,3,4,5};
public static void StrPassing(string someStr)
{
string otherStr="Changed!";
someStr=otherStr;
}
public static void NumPassing(int[] a)
{
a[2] = 115;
}
}
static void Main(string[] args)
{
Example ex = new Example();
Example.StrPassing(ex.str);
Example.NumPassing(ex.intValues);
foreach (int i in ex.intValues)
{
Console.WriteLine(i);
}
Console.WriteLine(ex.str);
Console.ReadLine();
}
intValues[2]的值在传递引用时更改为115。但是字符串“str”(demo)的值没有更改为“Changed!”。这是什么原因? .我可以把它当作数组是通过引用传递的,而其他引用类型是通过传递的
价值?
【问题讨论】: