使用 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 参数是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号