桦山涧

桦山涧
Asp.net ---->知识改变命运!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于传参数中的ref

Posted on 2007-11-07 19:45  桦林  阅读(643)  评论(0编辑  收藏  举报
ref  
  方法参数上的   ref   方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。  
   
  若要使用   ref   参数,必须将参数作为   ref   参数显式传递到方法。ref   参数的值被传递到   ref   参数。  
   
  传递到   ref   参数的参数必须最先初始化。将此方法与   out   参数相比,后者的参数在传递到   out   参数之前不必显式初始化。  
   
  属性不是变量,不能作为   ref   参数传递。  
   
  如果两种方法的声明仅在它们对   ref   的使用方面不同,则将出现重载。但是,无法定义仅在   ref   和   out   方面不同的重载。例如,以下重载声明是有效的:  
   
  class   MyClass    
  {  
        public   void   MyMethod(int   i)   {i   =   10;}  
        public   void   MyMethod(ref   int   i)   {i   =   10;}  
  }  
  但以下重载声明是无效的:  
   
  class   MyClass    
  {  
        public   void   MyMethod(out   int   i)   {i   =   10;}  
        public   void   MyMethod(ref   int   i)   {i   =   10;}  
  }  
  有关传递数组的信息,请参见使用   ref   和   out   传递数组。  
   
  示例  
  //   cs_ref.cs  
  using   System;  
  public   class   MyClass    
  {  
        public   static   void   TestRef(ref   char   i)    
        {  
              //   The   value   of   i   will   be   changed   in   the   calling   method  
              i   =   'b';  
        }  
   
        public   static   void   TestNoRef(char   i)    
        {  
              //   The   value   of   i   will   be   unchanged   in   the   calling   method  
              i   =   'c';  
        }  
   
        //   This   method   passes   a   variable   as   a   ref   parameter;   the   value   of   the    
        //   variable   is   changed   after   control   passes   back   to   this   method.  
        //   The   same   variable   is   passed   as   a   value   parameter;   the   value   of   the  
        //   variable   is   unchanged   after   control   is   passed   back   to   this   method.  
        public   static   void   Main()    
        {  
         
              char   i   =   'a';         //   variable   must   be   initialized  
              TestRef(ref   i);     //   the   arg   must   be   passed   as   ref  
              Console.WriteLine(i);  
              TestNoRef(i);  
              Console.WriteLine(i);  
        }  
  }  
  输出  
  b  
  b  


而讲到ref就必然要对比于out来讲
1、ref表示传址,out表示变量用于输出.
2、ref变量可以在传入函数前初始化. out变量不能在传入函数前初始化.
3、ref和out都是传值,所不同的是out不需要值在传入前初始化,因为你传进去的值在方法外初始化是无用的,因为它在方法内的第一步规定是要初始化参数的。  
  ref则不需要在方法开始时对参数进行初始化  
  ref和out显示了在现实世界中的实际行为:  
  ref代表了当某些行为是因为外界的某些参数的参与而改变行为的结果的。  
  out代表了无论外界因素如何变化,他都输出同样的结果,就好像法律是不以人的高低贵贱来判决的。
4、如下:


方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。ref 参数的值被传递到 ref 参数。 传递到 ref 参数的参数必须最先初始化。将此方法与 out 参数相比,后者的参数在传递到 out 参数之前不必显式初始化。属性不是变量,不能作为 ref 参数传递。

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

public static void MyMethod(out int[] arr)
{
   arr 
= new int[10];   // 数组arr的明确委派
}

2、与所有的 ref 参数一样,数组类型的 ref 参数必须由调用方明确赋值。因此不需要由接受方明确赋值。可以将数组类型的 ref 参数更改为调用的结果。例如,可以为数组赋以 null 值,或将其初始化为另一个数组。例如:
public static void MyMethod(ref int[] arr)
   {
  arr 
= new int[10];   // arr初始化为一个新的数组
    }

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

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

using System;
class TestOut
  {
   
static public void FillArray(out int[] myArray)
   
{
      
// 初始化数组(必须):
      myArray = new int[5]{12345};
   }


   
static public void Main()
   {
      
int[] myArray; // 初始化数组(不是必须的!)

      
// 传递数组给(使用out方式的)调用方:
      FillArray(out myArray);

      
// 显示数组元素
      Console.WriteLine("数组元素是:");
      
for (int i=0; i < myArray.Length; i++)
         Console.WriteLine(myArray[i]);
   }

}

输出
数组元素是:
1
2
3
4
5
示例 2
在此例中,在调用方(Main 方法)中初始化数组 myArray,并通过使用 ref 参数将其传递给 FillArray 方法。在 FillArray 方法中更新某些数组元素。然后将数组元素返回调用方并显示。
using System;
class TestRef
{
   
public static void FillArray(ref int[] arr)
  
{
      
// 根据需要创建一新的数组(不是必须的)
      if (arr == null)
         arr 
= new int[10];
      
// 否则填充数组,就可以了
      arr[0= 123;
      arr[
4= 1024;
   }


   
static public void Main ()
  
{
      
//初始化数组:
      int[] myArray = {1,2,3,4,5}

      
// 使用ref传递数组:
      FillArray(ref myArray);

      
//显示更新后的数组元素:
      Console.WriteLine("数组元素是:");
      
for (int i = 0; i < myArray.Length; i++)
         Console.WriteLine(myArray[i]);
   }

}

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