C#中ref、out及特殊说明参数的用法

 1.out参数的用法。

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]);
    }

}

2.ref参数的基本用法,相当于c里面的指针。


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]);
    }

}


3.有ref与无ref的比较。


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);
   i 
= 'a';  
    TestNoRef(i);
    Console.WriteLine(i);
      
  }

}
 


4.此处应用了类,注意类是引用类型,直接进行引用,除非在函数内生成新的类。



using  System;
//  ----------------------------------------
//  MyClass definition
public   class  MyClass
{
    
public int Value;



    
// ----------------------------------------
    
// Tester methods
    public static void TestRef(ref MyClass m)
    
{
        m.Value 
= 10;
    }


    
public static void TestNoRef(MyClass m)
    
{
        m.Value 
= 20;
    }


    
public static void TestCreateRef(ref MyClass m)
    
{
        m 
= new MyClass();
        m.Value 
= 100;
    }


    
public static void TestCreateNoRef(MyClass m)
    
{
        m 
= new MyClass();
        m.Value 
= 200;
    }


    
public static void Main()
    
{
        MyClass m 
= new MyClass();
        m.Value 
= 1;

        TestRef(
ref m);
        Console.WriteLine(m.Value);

        TestNoRef(m);
        Console.WriteLine(m.Value);

        TestCreateRef(
ref m);
        Console.WriteLine(m.Value);

        TestCreateNoRef(m);
        Console.WriteLine(m.Value);
    }

}


5.ref,out,与无参参数的比较。


using  System;
class  TestApp
{
    
static void outTest(out int x, out int y)
    
{//离开这个函数前,必须对x和y赋值,否则会报错。
        
//y = x;
        
//上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
        x = 1;
        y 
= 2;
    }

    
static void refTest(ref int x, ref int y)
    
{
        x 
= 1;
        y 
= x;
    }



    
static void noRefTest(int x, int y)
    
{
        x 
= 8;
        y 
= x;
    }




    
public static void Main()
    
{
        
//out test
        int a, b;
        
//out使用前,变量可以不赋值
        outTest(out a, out b);
        Console.WriteLine(
"a={0};b={1}", a, b);
        
int c = 11, d = 22;
        outTest(
out c, out d);
        Console.WriteLine(
"c={0};d={1}", c, d);

        
//ref test
      
//  int m, n;
        
//refTest(ref m, ref n);
        
//上面这行会出错,ref使用前,变量必须赋值

        
int o = 11, p = 22;
        refTest(
ref o, ref p);
        Console.WriteLine(
"o={0};p={1}", o, p);

        
int li = 99, zong = 89;
        noRefTest(li, zong);
        Console.WriteLine(
"{0},{1}", li, zong);



    }

}

































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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superdont

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值