C# FAQ: How do I declare an out variable?

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How do I declare an out variable?

An out parameter is used to return a value in the same variable which was passed in as a parameter to the method. Any changes made to the parameter are reflected in the variable.

public class Test
{
   public static void outMethod (out int param1, out int param2)
   {
      param1 = 1;
      param2 = 2;
   } 
 
   public static void Main()
   {
      int out1, out2;    // do not initialize variables
      outMethod (out out1, out out2));
      Console.WriteLine (out1); // outputs "1"
      Console.WriteLine (out2); // outputs "2"
   }
}

Use the out keyword on both the method declaration and invocation.

See also



Personal tools