Ref keyword usage, ref keyword usage
Ref
The keyword is used to pass parameters by reference (rather than value.
The effect passed by reference is that any changes made to the parameters in the called method are reflected in the call method.
For example, if the caller passes a local variable expression or an array element access expression, the called method replaces the object with the object referenced by the ref parameter, then, the caller's local variables or array elements start to reference the new object.
To useRef
Parameters, method definitions, and call methods must be explicitly usedRef
Keyword, as shown in the following example.
PassRef
Parameters must be initialized before being passed.
This correspondsOut
The parameters are different. You do not need to initialize the parameters explicitly before passing them. For more information, see out.
The class member cannot haveRef
AndOut
Different signatures.
If the only difference between two types of members is that one of them hasRef
Parameter, while the other hasOut
Parameter, a compilation error occurs.