out
keyword causes arguments to be passed by reference only difference is that out
parameters doesn\’t require initialization like ref
parameter and out parameter must assigned/initialize inside method.
Points to remeber
- The
ref
andout
keywords cause different run-time behaviour, they are not considered part of the method signature at compile time. You can not overload method byref
andout.
class Example { // Compiler error CS0663: "Cannot define overloaded // methods that differ only on ref and out". public void Method1(out int i) { i = 1; } public void Method1(ref int i) { } }
- Property, indexers, dynamic types can not be passed as
ref
orout
parameter.
Async
methods, which you define by using theasync
modifier can not useref
orout
.- Iterator methods, which include a yield return or yield break statement can not use
ref
orout
.