C# ref与out

时间:2020-06-02 18:57:43   收藏:0   阅读:66

参考链接

总结如下:

  • 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
    }
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!