首页 > Windows开发 > 详细

C#基本语法之ref和out

时间:2018-09-04 00:58:45      阅读:186      评论:0      收藏:0      [点我收藏+]

ref参数必须在初始化的时候为其赋值,否则会报错

static void Main(string[] args)
        {
//out需要在初始化时为变量赋值,如果初始的值为空则会报错
            int a = 6;
            int b = 66;
            Fun(ref a, ref b);
            Console.WriteLine("a:{0},b:{1}", a, b);
            Console.ReadLine();
        }
        static void Fun(ref int a, ref int b)
        { a= 2;
            b = 1;
        }

out参数在初始化时可以不赋值,但是在方法中必须初始化

 static void Main(string[] args)
        {

            int a =1;
            int b =2;
            Fun(out a, out b);
            Console.WriteLine("a:{0},b:{1}", a, b);
            Console.ReadLine();
        }
        static void Fun(out int a, out int b)
        {
            //a = a + b如果使用该种方式则编译器会提示使用了未赋值的参数
            a = 2;
            b = 1;
        }

注:ref关键字侧重于修改参数的值,而out关键字侧重于输出参数值

C#基本语法之ref和out

原文:https://www.cnblogs.com/Bala-PanXin/p/9581968.html

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