10 ref 和 out 之间的差别


(1) 两者都是按地址传递的,使用后都将改变原来的数值

(2) ref传进去的參数必须在调用前初始化,out不必
(3) ref传进去的參数在函数内部能够直接使用,而out不可
(4) ref传进去的參数在函数内部能够不被改动,但out必须在离开函数体前进行赋值

(5) ref是有进有出,out是仅仅出不进


        string outString = "This is the original outString";
        Console.WriteLine(outString);
        outMethod(out outString);
        Console.WriteLine(outString);

        string refString = "This is the original ref string";
        Console.WriteLine(refString);
        refMethod(ref refString);
        Console.WriteLine(refString);



原文地址:https://www.cnblogs.com/ldxsuanfa/p/9938568.html