日期:2014-05-18 浏览次数:20677 次
public static void NonRefArray(object[] a) { a[1] = 2; a = new object[] { 3, 3, 3 }; } public static void RefArray(ref object[] a) { a[1] = 2; a = new object[] { 3, 3, 3 }; } static void Main(string[] args) { object[] a = new object[] { 1, 1, 1, 1 }; NonRefArray(a); Console.WriteLine(String.Join(", ", a)); RefArray(a); Console.WriteLine(String.Join(", ", a)); }
------解决方案--------------------
// 修改一下: public static void NonRefArray(object[] a) { a[1] = 2; a = new object[] { 3, 3, 3 }; } public static void RefArray(ref object[] a) { a[1] = 2; a = new object[] { 3, 3, 3 }; } static void Main(string[] args) { object[] a = new object[] { 1, 1, 1, 1 }; NonRefArray(a); Console.WriteLine(String.Join(", ", a)); RefArray(ref a); // 调用加 ref 关键字 Console.WriteLine(String.Join(", ", a)); }