ref和out的用法及区别


ref:

ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该 变量中。简单点说就是,使用了 refout的效果就几乎和C中使用了 指针变量一样。它能够让你直接对原数进行操作,而不是对那个原数的Copy进行操作。
若要使用  ref 参数,则方法定义和调用方法都必须显式使用  ref 关键字。例如:
class RefExample
{
static void Method(ref int i)
{
i = 44;
}
static void Main()
{
int val = 0;
Method(ref val); // val is now 44 
}
}

out:
out形参类似于 ref形参,但它只能用来将值从方法中传出。在调用之前,并不需要为out形参的变量赋予初始值。在方法中,out形参总是被认为是未赋值的,但在方法结束前必须赋于形参一个值。因此,当调用方法后,out形参引用的变量会包含一个值。
下面是一个使用out形参例子。方法RectInfo()返回已知边长的矩形面积。在形参isSquare中,如果矩形是正方形,返回true,否则返回false。因此,RectInfo()向调用者返回两条信息。
using System;
class Rectangle{
int side1;
int side2;
public Rectangle(int i, int j){
side1 = i;
side2 = j;
}
// Return area and determine if square.
public int RectInfo(out bool isSquare){
if (side1 == side2) isSquare = true;
else isSquare = false;
return side1 * side2;
}
}
class OutDemo{
static void Main(){
Rectangle rect = new Rectangle(10, 23);
int area;
bool isSqr;
area = rect.RectInfo(out isSqr);
if (isSqr) Console.WriteLine("rect is a square.");
else Console.WriteLine("rect is not a square.");
Console.WriteLine("Its area is " + area + ".");
Console.ReadKey();
}
}

如果希望方法返回多个值,可以声明 out 方法。 下面的示例使用 out 返回具有单个方法调用的三个变量。  注意,第三个参数赋 null 值。  这使得方法可以有选择地返回值。

C#
    class OutReturnExample
    {
        static void Method(out int i, out string s1, out string s2)
        {
            i = 44;
            s1 = "I've been returned";
            s2 = null;
        }
        static void Main()
        {
            int value;
            string str1, str2;
            Method(out value, out str1, out str2);
            // value is now 44
            // str1 is now "I've been returned"
            // str2 is (still) null;
        }
    }



ref和out的区别:

ref是传递参数的地址,out是返回值,两者有一定的相同之处,不过也有不同点。


  1⃣️使用ref前必须对变量赋值,out不必须用(因为即使赋了值也给你清空)

  2⃣️out的函数会清空变量,即使变量已经赋值也不行

       3⃣️退出函数时所有out引用的变量都要赋值,ref引用的可以修改,也可以不修改。 

     ref 一般侧重于修改,out一般侧重于输出。

  区别可以参看下面的代码:

using System;
class TestApp
{
 static void outTest(out int x, out int y)
 { 
  //y = x; 

  //上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行 

     //离开这个函数前,必须对x和y赋值,否则会报错。

  x = 1;
  y = 2;
 }
 static void refTest(ref int x, ref int y)
 { 
  x = 1;
  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);
 }
}



使用时ref和out时注意:

传递到  ref 参数的参数必须最先初始化。这与  out 不同, out 的参数在传递之前不需要显式初始化。
尽管  ref 和  out 在运行时的处理方式不同,但它们在编译时的处理方式是相同的。因此,如果一个方法采用  ref 参数,而另一个方法采用  out 参数,则无法 重载这两个方法。
例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译以下代码:
class CS0663_Example
{
// compiler error CS0663: "cannot define overloaded 
// methods that differ only on ref and out" 
public void SampleMethod(ref int i)
{
}
public void SampleMethod( out int i)
{
}
}
但是,如果一个方法采用  ref 或  out 参数,而另一个方法不采用这两类参数,则可以进行 重载,如下所示:
class RefOutOverloadExample
{
public void SampleMethod(int i)
{
}
public void SampleMethod(ref int i)
{
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值