Introduced:
First look at an example:
1 class Program2 {3 Static voidMain (string[] args)4 {5 intNumber =Ten;6 intresult =Test (number);7Console.WriteLine ("Number={0},result={1}", number, result);8 Console.readkey ();9 }Ten One Static intTest (inta) A { -A = -; - returnA; the } -}
At this point, obviously get number=10,a=20; pass number to a in method test, change the value of a in method number, and the value will not change.
And then we make the following changes
Class program { static void Main (string[] args) { int number; int result = Test (number);//Error "Using unassigned local variable ' number '" Console.WriteLine ("Number={0},result={1}", Number, result); Console.readkey (); } static int Test (int a) { a = +; return A; } }
Obviously, it's wrong to use a variable without assigning a value.
Now we use the out code as follows:
Class program { static void Main (string[] args) { int number; int result = Test (out number);//Note the call should also be added out Console.WriteLine ("Number={0},result={1}", number, result); Console.readkey (); } static int Test (out int a) { A = 20;//if the parameter in the method is marked out, it must be assigned to a before it can be used. return A; } }
at this point the result is number=20,result=20, and once the out,a is used, it is not passed to the method but returns the value outward.
Instead of assigning the value of number to a, it is a connection to a, which must be assigned to a in the method test, which is equivalent to assigning a value to a number (that is, passing a reference to a).
Out Summary:
1. Before the parameters of the method are added out, the arguments must also be used out before the arguments, to indicate that this parameter is not used to pass in but is used for outgoing values.
2. If the parameter is passed in as out, it cannot be assigned to the pass-in.
3. In a method, a parameter that is decorated with an out must be assigned a value and must be assigned before it is used.
Ref
Class program { static void Main (string[] args) { int number = ten; int result = Test (ref number), or//note that the call is also required to add ref Console.WriteLine ("Number={0},result={1}", number, result); Console.readkey (); } static int Test (ref int a) { int b = A;
a=20; return b; } }
The result is: number=20,result=10; The value of number is passed to method test, and variable B is used, and a is assigned a value of 20 and passed out.
That is, ref can either pass in a parameter or be outgoing.
Summarize ref and Out
Out is used to pass in values, ref is bidirectional and can be passed in and out. (The principle is that a reference type is passed with a value type, and is passed as a value type when out and ref is used, and is passed as a reference type)
In the process of passing parameters, if there is a ref and out modifier, then changing the value of the parameter variable in the method will also change the value of the variable in the consumer method.
Examples of use of out
1. Microsoft provides a method of type int
Int. TryParse (string s,out int result)
First, explain the method.
The return value of this method is type bool. That is, the conversion succeeds returns true, the conversion failure returns false, and the conversion succeeds to return a value of Result,result, which is the converted value, and the conversion failure result is null
Here we write a method to implement this function
Class program { static void Main (string[] args) { string s = "123"; int result; if (Convertint (s, out result)) { Console.WriteLine ("Conversion succeeded" + result); } else { Console.WriteLine ("Conversion failed! "); } Console.readkey (); } static bool Convertint (string s,out int result) { try { result = Convert.ToInt32 (s); return true; } catch { result = 0;//Note Here, you must assign a value to the parameter result of the out modifier to return false;} }
You can use out and ref when you need to return multiple values.
You can use the following example practiced hand!
Sums the elements of an array and returns the maximum and minimum values with the array elements
The generality, difference and usage of C # out and ref