关于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();} } }
2026年09月27日 07:52
有2个网友回答
网友(1):

数组本身是按引用传递的。
而值类型,int,double 等是复制值传递的。

网友(2):

130*3=39084÷7=12