Unity3D(13)—out和ref参数

1.out参数

  • 作用

一个函数中如果返回多个不同类型的值,就需要用到out参数。

  • 要点

函数外可以不为变量赋值,而函数内必须为其赋值。

  • 语法

形参和实参前面都要加上out关键字。

  • 示例

        static void Main(string[] args)
        {
            int a ;
            int c ;
            int d ;
            int f ;
            Sum(out a, out c, out d, out f);
            Console.WriteLine(d);//输出6;
            Console.WriteLine(f); //输出3
            Console.ReadKey();
        }

        public static void Sum(out int a,out int b,out int sum,out int avg)
        {
            a = 2;
            b = a + 2;
            sum = a + b;
            avg = sum / 2;
        }

2.ref参数

  • 作用

将一个变量传入一个函数中进行"处理","处理"完成后,再将"处理"后的值带出函数。

  • 要点

形参和实参前面都要加上ref关键字

  • 示例

        static void Main(string[] args)
        {
            int a = 1;
            int b = 3;
            Sum(ref a, ref b);
            Console.WriteLine(a); //输出4
            Console.ReadKey();
        }
        public static void Sum(ref int a,ref int b)
        {
            a += b;
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容