Out and Ref C # basic review

In development usually do when calling a method return values ​​are obtained only get a return value, but sometimes we need to get more, such as: I want to return a string, but also want a List array, but a the method can have only one return, so in this case the official will give us a new way to achieve the results we want!

Ref both and compare Out

In common : out, refare passing references (memory addresses), will change the value of the original parameters after use.

Different point : refthat there is a carry out there, outis out without (that is, the parameters passed to the method a time, automatically clears all of the original, into a clean parameter): refcan the value of the parameter passed to the function, but can not by outthe method, a numerical passed, ref argument must be initialized before use, and does not require out (initialization with no birds, empty sooner or later).

 

Code Example:

Call the method, passing references, change the value of the string:

static  void RefOrOutTest () 
        { 
            String name = " John Doe " ; 
            RefTest ( REF name); 
            Console.WriteLine (name); // method after the call, the original value changes, printing: John Doe! 
        } 

        static  void RefTest ( REF  String STR ) 
        { 
            STR = " John Doe " ; 
        }
static  void RefOrOutTest () 
        { 
            String name = " John Doe " ; 
            OutTest ( OUT name); 
            Console.WriteLine (name); // method after the call, the original value changes, printing: John Doe 
        } 

        static  void OutTest ( OUT  String STR) 
        { 
            STR = " John Doe " ; 
        }

As can be seen, no matter with ref or out, the results are the same! The conditions are the same, at all times be initialized incoming assignment, and then re-enter the method assignment!

Further tests:

static  void RefOrOutTest () 
        { 
            String name = " John Doe " ; 
            RefTest ( REF name); 
        } 

        static  void RefTest ( REF  String STR) 
        { 
            Console.WriteLine (STR); // Print: Zhang             
        }

Write can still print out the original value, but the ref changed out, the compiler will report an error!

This is an accurate description of: out parameters when entering method, C # automatically clears all its references and pointers, so in out above example, it is necessary first to assign to the str parameter. Such as the following procedure.

 

static  void RefOrOutTest () 
        { 
            String name = " John Doe " ; 
            OutTest ( OUT name); 
        } 

        static  void OutTest ( OUT  String STR) 
        { 
            STR = " Wang Wu " ; 
            Console.WriteLine (STR); // Print: Wang Wu             
        }

Further tests:

static  void RefOrOutTest () 
        { 
            String name; 
            OutTest ( OUT name); 
            Console.WriteLine (name); // Print : Wang Wu 
        } 

        static  void OutTest ( OUT  String STR) 
        { 
            STR = " Wang Wu " ; 
        }

Re-initialized when you can not still pass with out! Let's look at the following error will be passed by ref:

static void RefOrOutTest()
        {
            string name;
            RefTest(ref name);
            Console.WriteLine(name);
        }

        static void RefTest(ref string str)
        {
            str = "王五";
        }    

Error:

As can be seen, in the absence of initialization, out can still pass, because even if you initialized or has been emptied, and ref can not, must be initialized before transfer

Finally, probably sum up: when we need to return multiple values, use out, the need to modify the original reference to modify the time with ref!

Guess you like

Origin www.cnblogs.com/dcy521/p/10949076.html