Use 'out' keyword (no need to assign because it is an out) : out « Language Basics « C# / CSharp Tutorial






  1. An out parameter is used to pass a value out of a method.
  2. It is not necessary to give the variable used as an out parameter an initial value.
  3. An out parameter is always considered unassigned.
  4. The method must assign the parameter a value prior to the method's termination.
using System;

class MainClass
{
  public static void Add(int x,int y, out int ans)
  {
    ans = x + y;
  }

  public static void Main() 
  {
    
    Console.WriteLine("Adding 2 ints using out keyword");
    int ans;
    Add(90, 90, out ans);
    Console.WriteLine("90 + 90 = {0}\n", ans);

  }
}
Adding 2 ints using out keyword
90 + 90 = 180








1.12.out
1.12.1.Use 'out' keyword (no need to assign because it is an out)
1.12.2.Ref and Out Parameters
1.12.3.Out Parameters
1.12.4.Use out
1.12.5.Use two out parameters