Introduction:
In: The process will not overwrite the content of In. The default delivery method is to transfer values to the function internally.
Out and out: The passed value will not be read by the process. When Out is passed in, the value of the parameter will be cleared, but the process can be written. Only go out but not in
ref: The value of the parameter can be passed into a function, the process can be read and written. There are in and out.
1. In
The In keyword causes the parameters to be passed by value. That is, transfer values to the function.
For example:
using System; class gump { public double square(double x) { x=x*x; return x; } } class TestApp { public static void Main() { gump doit=new gump(); double a=3; double b=0; (\"Before square->a={0},b={1}\",a,b);//a=3,b=0; b=( a); (\"After square->a={0},b={1}\",a,b);//a=3,b=9; } }
2. Ref
The ref keyword causes the parameters to be passed by reference. The effect is that when control is passed back to the call method, any changes to the parameters in the method will be reflected in the variable. To use the ref parameter, both the method definition and the method calling must explicitly use the ref keyword.
For example:
using System; class gump { public double square(double x) { x=x*x; return x; } } class TestApp { public static void Main() { gump doit=new gump(); double a=3; double b=0; (\"Before square->a={0},b={1}\",a,b);//a=3,b=0; b=( ref a); (\"After square->a={0},b={1}\",a,b);//a=9,b=9; } }
The parameters passed to the ref parameter must be initialized first. This is different from out, where the parameters of the latter do not need to be explicitly initialized before being passed.
3. out
The out keyword will cause the parameters to be passed by reference. This is similar to the ref keyword, the difference is that ref requires that variables must be initialized before being passed. To use the out parameter, both the method definition and the method calling must explicitly use the out keyword.
using System; class gump { public void math_routines(double x,out double half,out double squared,out double cubed) //Can be: public void math_routines(ref double x,out double half,out double squared,out double cubed)//However, this cannot be done: public void math_routines(out double x,out double half,out double squared,//out double cubed), in this example, because the output value must be assigned by x, x can no longer be the output value { half=x/2; squared=x*x; cubed=x*x*x; } } class TestApp { public static void Main() { gump doit=new gump(); double x1=600; double half1=0; double squared1=0; double cubed1=0; [Page] /* double x1=600; double half1; double squared1; double cubed1; */ (\"Before method->x1={0}\",x1); (\"half1={0}\",half1); (\"squared1={0}\",squared1); (\"cubed1={0}\",cubed1); doit.math_rountines(x1,out half1,out squared1,out cubed1); //At this time, the parameter values of Out modified have changed. (\"After method->x1={0}\",x1); (\"half1={0}\",half1); (\"squared1={0}\",squared1); (\"cubed1={0}\",cubed1); } }
Although variables passed as out parameter do not have to be initialized before passing, the method needs to be called to assign values before the method returns.
class OutExample { static void Method(out int i) { i = 44; } static void Main() { int value; Method(out value); // value is now 44 } }
4. Differences:
The difference between out is in C#, parameters can be passed through values or references. Passing parameters by reference allows function members to change the value of the parameter and keep that change. To pass parameters by reference, use the ref or out keywords. Both ref and out keywords can provide similar functions, and their functions are very similar to pointer variables in C.
2. When using ref-type parameters, the passed parameters must be initialized first. For out, it must be initialized in the method.
3. When using ref and out, the Ref or Out keywords must be added to the parameters of the method and when executing the method. to satisfy the match.
Suitable for use where multiple return values are needed, while ref is used when the method to be called needs to be called to modify the caller's reference.
5. The out method parameter keyword on the method parameter causes the method reference to the same variable passed to the method. When the control passes back to the call method, any changes made to the parameters in the method will be reflected in the variable.
The above is the role and difference between the C# keywords in, out, and ref introduced to you by the editor. I hope it will be helpful to you. Thank you very much for your support for my website!