C#中的 ref 和 out

使用C#一年多了,竟然是昨天才知道C# 中有 ref  和 out的用法
于是看了一会儿demo,大致搞懂是啥东西了。

这种方法的优点就是可以返回多个值,不再受函数返回一个值的限制,很爽。

其实也很简单,就好像C++里的&(引用)
传递的是实际参数的地址,修改参数值,就会导致直接将内存中该地址中的值修改掉
所以之后输出该参数的值将是修改之后的值

需要注意的是:属性不是变量,不能作为 out 参数传递。

还有如下重载也是不允许的
1 None.gif class  MyClass 
2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
3ExpandedSubBlockStart.gifContractedSubBlock.gif   public void MyMethod(out int i) dot.gif{i = 10;}
4ExpandedSubBlockStart.gifContractedSubBlock.gif   public void MyMethod(ref int i) dot.gif{i = 10;}
5ExpandedBlockEnd.gif}

6 None.gif

对于ref 和 out给个简单的demo
相信一看就明白了

 1 None.gif //  PassingParams2.cs 
 2 None.gif using  System;
 3 None.gif class  PassingValByRef
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5InBlock.gif    static void SquareIt(ref int x)
 6InBlock.gif    // The parameter x is passed by reference.
 7InBlock.gif    // Changes to x will affect the original value of myInt.
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        x *= x;
10InBlock.gif        Console.WriteLine("The value inside the method: {0}", x);
11ExpandedSubBlockEnd.gif    }

12InBlock.gif    public static void Main()
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        int myInt = 5;
15InBlock.gif        Console.WriteLine("The value before calling the method: {0}",
16InBlock.gif           myInt);
17InBlock.gif        SquareIt(ref myInt);   // Passing myInt by reference.
18InBlock.gif        Console.WriteLine("The value after calling the method: {0}",
19InBlock.gif           myInt);
20ExpandedSubBlockEnd.gif    }

21ExpandedBlockEnd.gif}
 

输出
The value before calling the method:  5
The value inside the method:  25
The value after calling the method:  25

代码讨论
本示例中,传递的不是 myInt 的值,而是对 myInt 的引用。参数 x 不是  int  类型,它是对  int  的引用(本例中为对 myInt 的引用)。因此,当在方法内对 x 求平方时,实际被求平方的是 x 所引用的项:myInt。


但是ref 和 out 也存在一些区别:

与所有的 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
}

下面给出两个demo

示例 1

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

 

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

 9InBlock.gif
10InBlock.gif   static public void Main() 
11ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
12InBlock.gif      int[] myArray; // Initialization is not required
13InBlock.gif
14InBlock.gif      // Pass the array to the callee using out:
15InBlock.gif      FillArray(out myArray);
16InBlock.gif
17InBlock.gif      // Display the array elements:
18InBlock.gif      Console.WriteLine("Array elements are:");
19InBlock.gif      for (int i=0; i < myArray.Length; i++)
20InBlock.gif         Console.WriteLine(myArray[i]);
21ExpandedSubBlockEnd.gif   }

22ExpandedBlockEnd.gif}

23 None.gif

输出
Array elements are:
1
2
3
4
5


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

 1 None.gif using  System; 
 2 None.gif class  TestRef 
 3 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 4InBlock.gif   public static void FillArray(ref int[] arr) 
 5ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
 6InBlock.gif      // Create the array on demand:
 7InBlock.gif      if (arr == null)
 8InBlock.gif         arr = new int[10];
 9InBlock.gif      // Otherwise fill the array:
10InBlock.gif      arr[0= 123;
11InBlock.gif      arr[4= 1024;
12ExpandedSubBlockEnd.gif   }

13InBlock.gif
14InBlock.gif   static public void Main () 
15ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
16InBlock.gif      // Initialize the array:
17ExpandedSubBlockStart.gifContractedSubBlock.gif      int[] myArray = dot.gif{1,2,3,4,5};  
18InBlock.gif
19InBlock.gif      // Pass the array using ref:
20InBlock.gif      FillArray(ref myArray);
21InBlock.gif
22InBlock.gif      // Display the updated array:
23InBlock.gif      Console.WriteLine("Array elements are:");
24InBlock.gif      for (int i = 0; i < myArray.Length; i++
25InBlock.gif         Console.WriteLine(myArray[i]);
26ExpandedSubBlockEnd.gif   }

27ExpandedBlockEnd.gif}

28 None.gif

输出

Array elements are:
123
2
3
4
1024
 


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值