码迷,mamicode.com
首页 > Windows程序 > 详细

C# ref与out

时间:2020-06-02 18:57:43      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:main   void   参考   logs   总结   http   class   链接   htm   

参考链接

总结如下:

  • ref 传入的时候,必须要对其赋值
  • out 离开的时候,必须要对其赋值

例子:

ref

class RefExample
{
    static void Method(ref int i)
    {
        i = 44;//可以不对i赋值
    }

    static void Main()
    {
        int val = 0;
        Method(ref val);
        // val is now 44
    }
}

out

class OutExample
{
    static void Method(out int i)
    {
        i = 44;  //必须对i赋值
    }

    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

C# ref与out

标签:main   void   参考   logs   总结   http   class   链接   htm   

原文地址:https://www.cnblogs.com/Alex-Mercer/p/13032789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!