Summary of differences between out and ref:
1. Both are passed by reference.
2. Both are passed by address and will change the value of the original parameter after use.
3. The property is not a variable and therefore cannot be passed as an out or ref parameter.
4. To use ref or out, both the method definition and the calling method must explicitly use the out, ref keyword.
5.rel can pass the value of the parameter into the function, but out is to clear the parameters, that is, you can not transfer a value from the out, after the out, the value of the parameter is empty, so you have to initialize it once. Or, Rel is there in and out and out is only out of the
6. Although ref and out are handled differently at run time, they are handled the same way at compile time.
-6-1. Therefore, if a method takes a ref parameter (example: Method (ref int i)) and the other method takes an out parameter (example: method (out int i)), the two methods cannot be overloaded because the above two methods are identical from a compilation point of view. Therefore the code inside the method will not be compiled
-6-2. However, if a method takes a ref or out parameter (for example: method (ref/out int i)) and the other method does not take these two parameter method (int i), you can overload
7. Practical occasions: Out is suitable for use where multiple return values need to be retrun, while ref is used to modify the caller's reference when the method needs to be called.
The code is as follows:
1PublicStaticvoid Main (String[] args)2{3//Use out4 Console.WriteLine ("-----------------------------using the Out method-----------------------------");5 Console.WriteLine ("-----------------The parameter is not assigned before calling the Out method-----------------");6StringOutname;7StringOutschool;8IntOutgrad;9 Outmethod (Out Outname,Out Outgrad,OutOutschool);Ten Console.WriteLine ("Students"+ Outname +"Is:"+ Outschool + Outgrad +"Level students");11Console.WriteLine ("-----------------called the Out method before the parameter is assigned a value-----------------");13String outnamevalue ="Li xx";14String outschoolvalue ="Henan University";15int outgradvalue =2011;Outmethod (Out Outnamevalue,Out Outgradvalue,OutOutschoolvalue);Console.WriteLine ("Students"+ Outnamevalue +"Is:"+ Outschoolvalue + outgradvalue +"Level students");18192021st//Using ref:Console.WriteLine ("-----------------------------the method of using ref-----------------------------");23String refName ="Li xx";24String Refschool ="Henan University";25int Refgrad =2011;Console.WriteLine ("-----------------The Refmethod method is not assigned a value-----------------");Refmethod (Ref RefName,Ref Refgrad,RefRefschool);Console.WriteLine ("Students"+ RefName +"Is:"+ Refschool + Refgrad +"Level students");Console.WriteLine ("-----------------the Refmethod method is assigned a value-----------------");Refmethodvalue (Ref RefName,Ref Refgrad,RefRefschool);Console.WriteLine ("Students"+ RefName +"Is:"+ Refschool + Refgrad +"Level students");3233Console.ReadLine ();34}35363738//Outmethod39//Out method output parameter characteristics:40//1. Do not allocate new memory for the parameter in the stack.41//2. The name of the formal parameter corresponds to the alias of the argument variable, referencing the same memory location as the argument.42//3. Since the parameters and arguments refer to the same memory location, any changes made by the formal parameters during the execution of the method are visible through the real parametric after the method completes.43PrivateStaticvoid Outmethod (out string name, out int Grad, out string School "44 { Span data-mce-= "" >45 name = " King Xx" ;< Span data-mce-= "" >46 Grad = 2009; School = "Zhengzhou University";}49//refmethod51//ref method output parameter characteristics: 52//1. Do not allocate new memory for the parameter in the stack. 53//2. The name of the parameter corresponds to the alias of the argument variable, referencing the same memory location as the argument. 54//3. Since the parameters and arguments refer to the same memory location, any changes made to the parameters during the execution of the method are visible through the real parametric after the method completes. The private static void Refmethod (ref string name, ref int Grad, ref string school),}59 private static void Refmet Hodvalue (ref string name, ref int Grad, ref string school): {"x" = "King xx"; grad = 2009;63 School = "Zhengzhou University";}
The output of the above code:
From the above results can be seen:
1, before calling the method Outmethod the value of the variable assignment does not have any meaning, because out is to empty the parameters, its parameter assignment as long as the inside of the method can be done;
2, the variable must be assigned before calling Refmethod, however, in the Refmethod method internal parameter assignment can not be assigned value, such as the internal value of the method is re-assigned to modify its value, such as the method is not re-assigned value is not modified its value; rel is there in and out,
C # 's Out and ref differences