In the study of C #, there are always some language commands such as the use of similar but not the same, today's discussion is
Out and ref in C #The difference between the first discussion is the respective usage, and below I will summarize the difference between out and ref. Apache PHP MySQL
Both are passed by address and will change the value of the original parameter after use.
Ref can pass the value of the parameter into the function, but out is to empty the parameter, that is, you cannot pass a value out of the out, after the out, the value of the parameter is empty, so you must initialize it once. This is the difference of two, or as some netizens say, ref is in there, out is only out.
Ref (C # Reference)
The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes to the parameters in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class RefExample
{
static void Method( ref int i)
{
i = 44;
}
static void Main()
{
int val = 0;
Method( ref val);
// val is now 44
}
}
|
The arguments passed to the ref parameter must be initialized first. This differs from out, whose arguments do not need to be explicitly initialized before being passed.
Although ref and out are handled differently at run time, they are handled the same way at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical, so the following code will not be compiled:
1 2 3 4 5 6 7 |
class CS0663_Example
{
// Compiler error CS0663: "cannot define overloaded
// methods that differ only on ref and out".
public void SampleMethod( ref int i) { }
public void SampleMethod( out int i) { }
}
|
However, if a method takes a ref or out parameter and the other method does not take both parameters, it can be overloaded, as shown in the following example:
1 2 3 4 5 |
class RefOutOverloadExample
{
public void SampleMethod( int i) { }
public void SampleMethod( ref int i) { }
}
|
Out (C # Reference)
The Out keyword causes parameters to be passed by reference. This is similar to the REF keyword, except that the ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the Out keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class OutExample
{
static void Method( out int i)
{
i = 44;
}
static void Main()
{
int value;
Method( out value);
// value is now 44
}
}
|
Although a variable passed as an out parameter does not have to be initialized before it is passed, the method needs to be called to assign a value before the method returns.
The ref and out keywords are handled differently at run time, but are handled the same way at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical, so the following code will not be compiled:
1 2 3 4 5 6 7 |
class CS0663_Example
{
// Compiler error CS0663: "Cannot define overloaded
// methods that differ only on ref and out".
public void SampleMethod( out int i) { }
public void SampleMethod( ref int i) { }
}
|
However, if a method takes a ref or out parameter and the other method does not take these two types of arguments, it can be overloaded as follows:
1 2 3 4 5 |
class RefOutOverloadExample
{
public void SampleMethod( int i) { }
public void SampleMethod( out int i) { }
}
|
Here are some of my experiences:
Difference
The difference between ref and out is that in C #, parameters can be passed either by value or by reference. Passing parameters by reference allows a function member to change the value of a parameter and keep the change. To pass parameters by reference, you can use the ref or out keyword. The two keywords, ref and out, can provide similar effects and act like pointer variables in C. The difference between them is:
1. When using a ref parameter, the passed-in parameter must first be initialized. For out, initialization must be done in the method.
2. When using ref and out, the ref or out keyword is added to the method's parameters and execution methods. To meet the match.
3. Out is suitable for use where multiple return values need to be retrun, while ref is used to modify the caller's reference in a method that needs to be called.
The Out method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.
Declaring an out method is useful when you want a method to return more than one value. Methods that use out parameters can still return a value. A method can have more than one out parameter.
To use an out parameter, you must explicitly pass the parameter to the method as an out parameter. The value of the out parameter is not passed to the out parameter.
You do not have to initialize a variable passed as an out parameter. However, you must assign a value to the out parameter before the method returns.
property is not a variable and cannot be passed as an out parameter.
In short, in a nutshell is: Ref is in and out, and out is only out.
Related articles:
C # Tutorial C # data type
Viewing the characteristics of c,c++,c#,java,php by static local variables