首页 > 其他 > 详细

out 和 ref 区别

时间:2021-05-17 00:40:17      阅读:18      评论:0      收藏:0      [点我收藏+]
class Program
{
    static void Main(string[] args)
    {
        int number = 50;
        Console.WriteLine("调用方法前 number 值:" + number);
        RefFunction(ref number);
        Console.WriteLine("调用方法后 number 值:" + number);
        Console.WriteLine();

        int number2 = 50;
        Console.WriteLine("调用方法前 number 值:" + number2);
        OutFunction(out number2);
        Console.WriteLine("调用方法后 number 值:" + number2);
        Console.WriteLine();
    }

    // 传入的参数值是 50 ,方法中使用的num值也是50
    static void RefFunction(ref int num)
    {
        num = num / 2;
    }

    // 无法将的参数值50传入 ,但是必须在方法中初始化
    static void OutFunction(out int num)
    {
        //初始化
        num = 120;
        num = num / 2;
    }
}

运行:

技术分享图片

 

out 和 ref 区别

原文:https://www.cnblogs.com/Allofus/p/14774691.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!