c#学习体会:使用 ref 和 out 传递数组(downmoon),希望与大家分享
1、与所有的 out 参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由接受方为其赋值。例如:

c#学习体会:使用 ref 和 out 传递数组public static void MyMethod(out int[] arr)
}

2、与所有的 ref 参数一样,数组类型的 ref 参数必须由调用方明确赋值。因此不需要由接受方明确赋值。可以将数组类型的 ref 参数更改为调用的结果。例如,可以为数组赋以 null 值,或将其初始化为另一个数组。例如:
c#学习体会:使用 ref 和 out 传递数组public static void MyMethod(ref int[] arr)
}

下面的两个示例说明 out 和 ref 在将数组传递给方法上的用法差异。

示例 1
在此例中,在调用方(Main 方法)中声明数组 myArray,并在 FillArray 方法中初始化此数组。然后将数组元素返回调用方并显示。

c#学习体会:使用 ref 和 out 传递数组using System;
c#学习体会:使用 ref 和 out 传递数组
class TestOut
}

输出
数组元素是:
1
2
3
4
5
示例 2
在此例中,在调用方(Main 方法)中初始化数组 myArray,并通过使用 ref 参数将其传递给 FillArray 方法。在 FillArray 方法中更新某些数组元素。然后将数组元素返回调用方并显示。
c#学习体会:使用 ref 和 out 传递数组using System;
c#学习体会:使用 ref 和 out 传递数组
class TestRef
}

输出
数组元素是:
123
2
3
4
1024

相关文章:

  • 2021-05-23
  • 2021-10-09
  • 2021-12-10
  • 2021-07-14
  • 2021-08-31
  • 2021-06-20
  • 2022-12-23
猜你喜欢
  • 2021-08-29
  • 2022-12-23
  • 2021-08-14
  • 2021-08-19
相关资源
相似解决方案