关于C#中,为何不用引用调用直接把一个数组值传递到方法里面,执行方法后该数据也能发生变化?
namespace ch5_TestDelegate{ class Test { public static bool SortArray( int[] array) { for (int i = array.GetUpperBound(0); i >= 0; i--) { for (int j = 0; j <= i; j++) { if (array[j]<=array[i]) swap(ref array[j],ref array[i]); } } return true; } static void swap(ref int x,ref int y) { int temp=x; x=y; y=temp; }}class program { static void Main(string[] args) { int[] arr=new int[]{8,9,5,7,2,1,4,5,6}; Console. WriteLine("排序前的数组元素是"); foreach (int i in arr) //遍历数组 {Console.Write("{0} ",i);} Test.SortArray(arr); Console.WriteLine("尀n排序后的数组元素是"); foreach(int i in arr){Console.Write("{0} ",i);} Console.ReadLine();} } }