【C#基础】ref 和 out 的区别

用途:
在C#中通过使用方法来获取返回值时,通常只能得到一个返回值。因此,当一个方法需要返回多个值的时候,就需要用到ref和out。

正确的使用ref:

class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            int x = 10;
            int y = 20;
            pg.GetValue(ref x, ref  y);
            Console.WriteLine("x={0},y={1}", x, y);
 
            Console.ReadLine();
        }
 
        public void GetValue(ref int x, ref int y)
        {
            x = 333;
            y = 444;
        }
    }

运行结果为
x=521,y=520

错误的使用ref:

class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            int x;
            int y;        //此处x,y没有进行初始化,则编译不通过。
            pg.GetValue(ref x, ref  y);
            Console.WriteLine("x={0},y={1}", x, y);
 
            Console.ReadLine();
        }
 
        public void GetValue(ref int x, ref int y)
        {
            x = 1000;
            y = 1;
        }    
    }

总结:使用ref 必须在 调用方法前 对其进行初识化操作

正确的使用out:

class Program
{
    static void Main(string[] args)
    {
        Program pg = new Program();
        int x = 10;
        int y = 233;
        Swap(out x, out y);
        Console.WriteLine("x={0},y={1}", x, y);

        Console.ReadLine();
    }

    public static void Swap(out int a, out int b)
    {

        a = 333;   //对a,b 在方法内进行了初识化,不会报错
        b = 444;
    }
}

运行结果为
在这里插入图片描述

错误的使用out:

class Program
{
    static void Main(string[] args)
    {
        Program pg = new Program();
        int x = 10;
        int y = 233;
        pg.Swap(out x, out y);
        Console.WriteLine("x={0},y={1}", x, y);

        Console.ReadLine();
    }

    public static void Swap(out int a, out int b)
    {
        int temp = a;   //a,b在函数内部没有赋初值,则出现错误。
        a = b;          //a,b 没有进行 初始化 ,会报错
        b = temp;
    }
}

总结:out 的使用必须要在 方法内 进行 初始化 ,才不会报错

口诀:
ref有进有出,out(有出去的意思)只出不进。

相同点:
1、都能返回多个返回值。

2、若要使用 ref 和out参数,则方法定义和调用方法都必须显式使用 ref和out 关键字。在方法中对参数的设置和改变将会直接影响函数调用之处(参数的初始值)。

不同点:

1、ref指定的参数在 函数调用前必须初始化,不能为空的引用。

2、out指定的参数在进入函数时会清空自己,必须在函数内部(即方法中)赋初值。

ref 和 out 本质上都是引用的传递

out 例子

public class Program
    {
        static void Main(string[] args)
        {
            string message = "你好!";  //原值
            SendMessage(out message);
            //使用了out,原值被更改了,
            //使用out 必须在方法体内初始化值,在外面可以不用
            Console.WriteLine(message); 
            Console.Read();//将当前窗体保留

        }
        public static void SendMessage(out string message) {

            message = "不好!";
            Console.WriteLine(message);
           
        }

    }

在这里插入图片描述

ref 例子

 public class Program
    {
        static void Main(string[] args)
        {
            string message = "你好!";  //原值
            SendMessage(ref message);
            //使用了ref,如果方法内进行了修改,原值就被修改了
            //使用ref 必须在 进入 方法前初始化值。
            Console.WriteLine(message); 
            Console.Read();//将当前窗体保留

        }
        public static void SendMessage(ref string message) {

            message = "不好!";
            Console.WriteLine(message);
           
        }

    }
    

在这里插入图片描述

多个返回值举例(以 ref 为例)

 public class Program
    {
        static void Main(string[] args)
        {
            string message1 = "你好!";  
            string message2 = "小明!";  
            SendMessage(message1,ref message2);
            Console.WriteLine(message1); //返回值1
            Console.WriteLine(message2); //返回值2
            Console.Read();//将当前窗体保留

        }
        public static string SendMessage(string message1,ref string message2) {

            message2 = message2 + "对message2进行处理";
            return message1;
           
        }

    }

在这里插入图片描述
out 同理

参考博客:https://blog.csdn.net/qq373011556/article/details/81944690

  • 17
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值