It is always known that both ref and out are reference types for passing values. But it's not clear about the differences between them. Today we have a little bit of knowledge.
Everyone knows that if you want to pass an address when passing parameters, you can use the ref or out keyword. In fact, both are transmitted by address, and the original values will be changed after use.
Main differences:
Ref can pass the value of a parameter to the function. However, an out parameter must be assigned a value before it can be used. This overwrites the original value.
In short, there is an inbound/outbound ref, but an outbound ref does not.
For example, in the following two methods, valuewithref does not need to be assigned a value again, but valuewithout will be compiled incorrectly if it is not assigned a value again for I.
// Normal operation, the output result is still the value of I at the time of input
Static Void Valuewithref ( Ref Int I)
{
Console. writeline (I. tostring ());
}
// Error
Static Void Valuewithout ( Out Int I)
{
// I = 200; // If this row is commented out, an error is returned.
Console. writeline (I. tostring ());
}