C# ref and out

 pass parameters with reference:

ref and out same:

both the method definition and the calling method must explicitly use the ref keyword.

both the method definition and the calling method must explicitly use the out keyword

static void Method(out int i, out string s1, out string s2)
    {
        i = 44;
        s1 = "I've been returned";
        s2 = null;
    }
    static void Main()
    {
        int value;
        string str1, str2;
        Method(out value, out str1, out str2);
        // value is now 44
        // str1 is now "I've been returned"
        // str2 is (still) null;
    }

Different:

An argument passed to a ref parameter must first be initialized. This differs from out, whose argument need not be explicitly initialized before being passed.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章