什么是 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();
}
}
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP