Definition of Methods That Take Arguments By Reference
Arguments to functions are normally passed by value. For value types, a function creates and operates on copy of the object passed in, so the value of the original object is unchanged by the function. For reference types, the function operates directly on the referenced object, so the referenced object can be modified. However, when passed by value, the reference itself cannot be changed.
Passing value types by reference means that functions operate directly on the object passed in, so any changes within the function affect the original object. Passing reference types by reference means that the reference can be changed by the function to refer to another object or null.
In a function declaration, it is possible to declare that parameters are to be passed by reference by using the /** @ref */ tag applied to the parameter. The tag must be ahead of any other modifiers applied to the parameter, as in the following example:
void f(/** @ref */ int i );
The /** @ref */ tag can be applied to primitive types, arrays, user-defined value types, enum types, and other reference types. It is an error to tag the arguments when calling the method.
It is an error to declare to overloaded methods that differ only in whether a parameter is passed by reference or not. At the point of the call, there would be no way to distinguish the two.
The /** @ref */ tag cannot be used with the /x:net option.
Example
// ByRef.jsl
import System.*;
public class C
{
public static final int MAX = 10;
public static void Increment(/** @ref */ int i)
{
i++;
}
public static void main()
{
for (int i = 0; i <= MAX; C.Increment(i))
{
Console.WriteLine("Value: " + i);
}
}
}
Output
Value: 0 Value: 1 Value: 2 Value: 3 Value: 4 Value: 5 Value: 6 Value: 7 Value: 8 Value: 9 Value: 10
See Also
Tasks
Calling Methods That Take Arguments By Reference
Reference
Syntax for Targeting the .NET Framework
Language Extensions to Support the .NET Framework