Categories
csharp

C# out parameter modifier

Csharp out parameter details. How out works and when we can use ref parameters.

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 and out keywords cause different run-time behaviour, they are not considered part of the method signature at compile time. You can not overload method by ref and out.
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 or out parameter.
  • Async methods, which you define by using the async modifier can not use ref or out.
  • Iterator methods, which include a yield return or yield break statement can not use ref or out.