C# ref与out

参考链接

总结如下:

  • 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
    }
}
posted @ 2020-06-02 18:02  Alex_Mercer  阅读(84)  评论(0编辑  收藏  举报