C#中的 ref 和 out

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

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

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

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

还有如下重载也是不允许的
1class MyClass 
2{
3   public void MyMethod(out int i) {i = 10;}
4   public void MyMethod(ref int i) {i = 10;}
5}

6

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

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

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

21}
 

输出
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 方法中初始化此数组。然后将数组元素返回调用方并显示。

 

 1using System; 
 2class TestOut 
 3{
 4   static public void FillArray(out int[] myArray) 
 5   {
 6      // Initialize the array:
 7      myArray = new int[5{12345};
 8   }

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

22}

23

输出
Array elements are:
1
2
3
4
5


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

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

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

27}

28

输出

Array elements are:
123
2
3
4
1024

 


 



posted on 2005-07-18 10:44  秋雨飘飞  阅读(1170)  评论(2编辑  收藏  举报