RefKeyword to pass Parameters by reference. The effect is that when the control is passed back to the call method, any changes made to the parameters in the method will be reflected in the variable. To use
RefParameter, the method definition and call method must be explicitly usedRefKeyword. For example:
Other
class RefExample{ static void Method(ref int i) { i = 44; } static void Main() { int val = 0; Method(ref val); // val is now 44 }}
PassRefThe parameter must be initialized first. Unlike out, out parameters do not need to be explicitly initialized before being passed. (See
Out .)
AlthoughRefAndOutThe processing methods are different at runtime, but they are the same at compilation. Therefore, if a method uses
RefParameter, while the other method uses
The out parameter cannot be used to reload the two methods. For example, from the compilation perspective, the two methods in the following code are identical, so the following code is not compiled:
Other
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 usesRefOr
The out parameter, and the other method does not use these two types of parameters, You can overload it, as shown below:
Other
class RefOutOverloadExample{ public void SampleMethod(int i) { } public void SampleMethod(ref int i) { }}Note:
Attribute is not a variable, so it cannot be used
RefParameter transfer.
For information on passing arrays, see passing Arrays Using ref and out.