Common Type System—Output Parameters
Output Parameters (Language Feature)
All parameters are input by default. That is, their values are supplied to the method by callers, but changes cannot be communicated back. We saw already that passing something by reference changes this. You can view the return value of a method as a special type of parameter, namely one that supplies a value to the caller from the callee but does not accept input at method invocation time. There is one significant limitation with a return parameter: You can only have one.
The C# language enables you to create additional output parameter on your method, which is simply a special form of the pass-by-reference. The difference between an ordinary byref and an output parameter is that C# permits you to pass a reference to a memory location not specifically initialized by the user program, doesn’t permit the receiving method to read from that reference until it’s been assigned by the receiver, and guarantees that the receiving method writes something to it before returning normally.
For example, consider this case:
public void Next3(int a, out int x, out int y, out int z) { x = a + 1; y = a + 2; z = a + 3; }
This example just assigns the next three integers after the input parameter a to the output parameters x,
y, and z, respectively. Similar to the case with pass-by-reference parameters, the caller needs to explicitly
state that he or she wishes to pass an argument as an out:
int a, b, c; Next3(0, out a, out b, out c);
The C# compiler generates code that, again, simply uses load address instructions.
|

