I. Ref keywordsInt number = 2;
// The address of the variable number is passed.
Getdouble (ref number );
// The value is changed and the result output is 4
Console. writeline (number );
Static int getdouble (ref int num)
{
// The value of the original reference parameter is modified.
Return num = num * 2;
}
Ii. Out keywords
Static void changevalue (Out int PARAM)
{
// Internal function value assignment. If the value is not re-assigned, an error occurs!
Param = 2;
}
Static void main (string [] ARGs)
{
// You do not need to assign an initial value like ref.
Int number;
// Reference the same number
Changevalue (Out int number );
// Finally obtain the output value after the change from the function. The result is 2.
Console. writeline (number );
}
Summary:The difference between keyword ref and out. To put it simply,
1) ref refers to transferring the reference of a variable to the function. The variable must be assigned a value before passing the parameter;
2) The out variable must be initialized in the function and then returned to the outside. When the out keyword is used, you do not need to assign a value to the parameter before passing the parameter. Even if the parameter is assigned, the value must be re-assigned within the function.