Indeed, there are 3 ways to pass a parameter to a method: by reference, by value, and as output.
By default, it does not have a keyword in C # (it works in VB.Net: ByVal ) - it passes a copy of value types:
public void SomeMethod1(int num) { num = 2; } int myNum = 1; SomeMethod1( myNum );
Vaguely - by value is passed a copy of the link for reference types. This means that your changes in the reference type are reverted to the instance, but you only have a copy of the actual pointer to the link:
public void SomeMethod1(MyClass instance) {
So, now the link to the value for structs is passed by reference ( ref in C # or ByRef in VB.Net):
public void SomeMethod1(ref int num) { num = 2; } int myNum = 1; SomeMethod1( ref myNum );
Simple enough, but for reference types, the actual pointer to the instance passes through the link, not the copy:
public void SomeMethod1(ref MyClass instance) {
Thus, although both by reference and by value are similar for reference types, the behavior is very different if you change the link itself (and not what you mean).
Finally, an additional return variable is used as the output, as is the actual return. These two base values โโare the same:
public int SomeMethod1() { return 1; } public void SomeMethod2(out int num) { num = 1; }
If you have an out parameter, it must be populated with a method (like returning).