C# 中数组类型的引用/ref 参数是什么?
使用 ref 关键字声明引用参数。引用参数是对变量内存位置的引用。当您通过引用传递参数时,与值参数不同,不会为这些参数创建新的存储位置。
声明引用参数 -
public void swap(ref int x, ref int y) {}登录后复制
static void Display(ref int[] myArr)登录后复制
class TestRef { static void Display(ref int[] myArr) { if (myArr == null) { myArr = new int[10]; } myArr[0] = 345; myArr[1] = 755; myArr[2] = 231; } static void Main() { int[] arr = { 98, 12, 65, 45, 90, 34, 77 }; Display(ref arr); for (int i = 0; i < arr.Length; i++) { System.Console.Write(arr[i] + " "); } System.Console.ReadKey(); } }登录后复制