使用 ref 和 out 传递数组

本文探讨了C#中数组作为方法参数的两种方式:使用out参数和ref参数。通过具体示例,详细解释了如何在方法中初始化和修改数组,以及调用者如何接收这些变化。

 

与所有的 out 参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由接受方为其赋值。例如:

public static void MyMethod(out int[] arr) 
{
   arr = new int[10];   // definite assignment of arr
}

与所有的 ref 参数一样,数组类型的 ref 参数必须由调用方明确赋值。因此不需要由接受方明确赋值。可以将数组类型的 ref 参数更改为调用的结果。例如,可以为数组赋以 null 值,或将其初始化为另一个数组。例如:

public static void MyMethod(ref int[] arr) 
{
   arr = new int[10];   // arr initialized to a different array
}

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

示例 1

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

None.gif //  cs_array_ref_and_out.cs
None.gif
using  System; 
None.gif
class  TestOut 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
static public void FillArray(out int[] myArray) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
// Initialize the array:
ExpandedSubBlockStart.gifContractedSubBlock.gif
      myArray = new int[5dot.gif{12345};
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
static public void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
int[] myArray; // Initialization is not required
InBlock.gif
InBlock.gif      
// Pass the array to the callee using out:
InBlock.gif
      FillArray(out myArray);
InBlock.gif
InBlock.gif      
// Display the array elements:
InBlock.gif
      Console.WriteLine("Array elements are:");
InBlock.gif      
for (int i=0; i < myArray.Length; i++)
InBlock.gif         Console.WriteLine(myArray[i]);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif

输出
Array elements are:
1
2
3
4
5
示例 2

在此例中,在调用方(Main 方法)中初始化数组 myArray,并通过使用 ref 参数将其传递给 FillArray 方法。在 FillArray 方法中更新某些数组元素。然后将数组元素返回调用方并显示。

None.gif //  cs_array_ref_and_out2.cs
None.gif
using  System; 
None.gif
class  TestRef 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public static void FillArray(ref int[] arr) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
// Create the array on demand:
InBlock.gif
      if (arr == null)
InBlock.gif         arr 
= new int[10];
InBlock.gif      
// Otherwise fill the array:
InBlock.gif
      arr[0= 123;
InBlock.gif      arr[
4= 1024;
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
static public void Main () 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
// Initialize the array:
ExpandedSubBlockStart.gifContractedSubBlock.gif
      int[] myArray = dot.gif{1,2,3,4,5};  
InBlock.gif
InBlock.gif      
// Pass the array using ref:
InBlock.gif
      FillArray(ref myArray);
InBlock.gif
InBlock.gif      
// Display the updated array:
InBlock.gif
      Console.WriteLine("Array elements are:");
InBlock.gif      
for (int i = 0; i < myArray.Length; i++
InBlock.gif         Console.WriteLine(myArray[i]);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
输出
Array elements are:
123
2
3
4
1024
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值