C# 中数组类型的引用/ref 参数是什么?

C# 中数组类型的引用/ref 参数是什么?

使用 ref 关键字声明引用参数。引用参数是对变量内存位置的引用。当您通过引用传递参数时,与值参数不同,不会为这些参数创建新的存储位置。

声明引用参数 -

public void swap(ref int x, ref int y) {}

登录后复制

声明数组类型的 ref 参数 -

static void Display(ref int[] myArr)

登录后复制

以下示例展示了如何在 C# 中使用数组类型的 ref 参数 -

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();
}
}

登录后复制

以上就是C# 中数组类型的引用/ref 参数是什么?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!