ref和out

ref 关键字就像c++里面的&,作用是声明,说明这个参数是传值调用的,也就是说,并不是把实参重新拷贝一份在函数里运行,最后函数出口的地方把参数析构。而是,传递的市参数的地址,在函数里面操作的都是参数的本身。下面这个例子可以说明。

using System;

namespace ConsoleApplication4
{
 class Class1
 {
  private static int a=3;
  private static int b=5;
  static void Main(string[] args)
  {
   OutPut();
   swap(ref a,ref b);
   OutPut();
  }

  private static void swap(ref int a ,ref int b){
   int temp=b;
   b=a;
   a=temp;   
  }

  private static void OutPut(){
   Console.WriteLine("a={0};b={1}",a,b);
  }

 }
}

结果

a=3;b=5

a=5;b=3

如果不用ref关键字

a=3;b=5

a=3;b=5

-------------------------------------------------------------------------------------------------------------------------------------------------

out关键字和ref关键字很像,但是有两点不同,下面先举个例子,然后说明不同

csdn例子:

using System;
public class MyClass
{
   public static int TestOut(out char i)
   {
      i = 'b';
      return -1;
   }

   public static void Main()
   {
      char i;   // variable need not be initialized
      Console.WriteLine(TestOut(out i));
      Console.WriteLine(i);
   }
}

我的例子:

using System;

namespace ConsoleApplication4
{
 class Class1
 {
  private static int a;
  private static int b;
  static void Main(string[] args)
  {
   //OutPut();
   swap(out a,out b);
   OutPut();
  }

  private static void swap(out int a ,out int b){
   //int temp=b;
   b=5;
   a=3;   
  }

  private static void OutPut(){
   Console.WriteLine("a={0};b={1}",a,b);
  }

 }
}

两点不同

通俗的说来就是ref函数外面就有值的,out必须原来没有被赋值,后来在函数体里面赋值然后再使用。

out必须在函数体里给参数赋值

A variable passed as an out argument need not be initialized. However, the out parameter must be assigned a value before the method returns.

属性不能作为out的参数进行传递

A property is not a variable and cannot be passed as an out parameter.

需要注意的一点是

An overload will occur if declarations of two methods differ only in their use of out. However, it is not possible to define an overload that only differs by ref and out. For example, the following overload declarations are valid:

两个函数不能因为ref和out进行重载。

下面来谈谈传递数组参数

Example 1

In this example, the array myArray is declared in the caller (the Main method), and initialized in the FillArray method. Then, the array elements are returned to the caller and displayed.

// cs_array_ref_and_out.cs
using System; 
class TestOut 
{
   static public void FillArray(out int[] myArray) 
   {
      // Initialize the array:
      myArray = new int[5] {1, 2, 3, 4, 5};
   }

   static public void Main() 
   {
      int[] myArray; // Initialization is not required

      // Pass the array to the callee using out:
      FillArray(out myArray);

      // Display the array elements:
      Console.WriteLine("Array elements are:");
      for (int i=0; i < myArray.Length; i++)
         Console.WriteLine(myArray[i]);
   }
}
Output
Array elements are:
1
2
3
4
5
out是只被声明但是没有被赋值,然后再里面赋的值。
Example 2

In this example, the array myArray is initialized in the caller (the Main method), and passed to the FillArray method by using the ref parameter. Some of the array elements are updated in the FillArray method. Then, the array elements are returned to the caller and displayed.

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

   static public void Main () 
   {
      // Initialize the array:
      int[] myArray = {1,2,3,4,5};  

      // Pass the array using ref:
      FillArray(ref myArray);

      // Display the updated array:
      Console.WriteLine("Array elements are:");
      for (int i = 0; i < myArray.Length; i++) 
         Console.WriteLine(myArray[i]);
   }
}
Output
Array elements are:
123
2
3
4
1024
ref可以原来赋过值,也可能原来没有赋过值,总之传的是地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值