The ref method parameter keyword in the method parameter allows the method reference to pass to the same variable of the method. When the control is passed back to the call method, any changes made to the parameters in the method will be reflected in this variable.
To use the ref parameter, you must explicitly pass the parameter as the ref parameter to the method. The ref parameter value is passed to the ref parameter.
Parameters passed to the ref parameter must be initialized first. Compared with the out parameter, the latter parameter does not need to be explicitly initialized before being passed to the out parameter.
Attributes are not variables and cannot be passed as ref parameters.
If the declarations of the two methods are only different in their use of ref, an overload will occur. However, it is not possible to define a unique reload for ref and out. For example, the following overload declaration is valid:
Class MyClass
{
Public void MyMethod (int I) {I = 10 ;}
Public void MyMethod (ref int I) {I = 10 ;}
}
However, the following overload statement is invalid:
Class MyClass
{
Public void MyMethod (out int I) {I = 10 ;}
Public void MyMethod (ref int I) {I = 10 ;}
}
The out method parameter keyword on the method parameter enables the method reference to pass to the same variable of the method. When the control is passed back to the call method, any changes made to the parameters in the method will be reflected in this variable.
When you want the method to return multiple values, it is very useful to declare the out method. You can still return a value using the out parameter. A method can have more than one out parameter.
To use the out parameter, you must use the parameter as the out parameter to pass it to the method explicitly. The value of the out parameter is not passed to the out parameter.
You do not need to initialize the variable passed as the out parameter. However, the out parameter must be assigned a value before the method is returned.
The property is not a variable and cannot be passed as an out parameter.
If the declaration of the two methods is only different in the use of out, the overload will occur. However, it is not possible to define a unique reload for ref and out. For example, the following overload declaration is valid:
Class MyClass
{
Public void MyMethod (int I) {I = 10 ;}
Public void MyMethod (out int I) {I = 10 ;}
}
The following overload statement is invalid:
Class MyClass
{
Public void MyMethod (out int I) {I = 10 ;}
Public void MyMethod (ref int I) {I = 10 ;}
}