Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

out

The out method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method.

Declaring an out method is useful when you want a method to return multiple values. A method that uses an out parameter can still return a value. A method can have more than one out parameter.

To use an out parameter, the argument must explicitly be passed to the method as an out argument. The value of an out argument will not be passed to the out parameter.

A variable passed as an out argument need not be initialized. However, the out parameter must be assigned a value before the method returns.

A property is not a variable and cannot be passed as an out parameter.

An overload will occur if declarations of two methods differ only in their use of out. However, it is not possible to define an overload that only differs by ref and out. For example, the following overload declarations are valid:

class MyClass 
{
   public void MyMethod(int i) {i = 10;}
   public void MyMethod(out int i) {i = 10;}
}

while the following overload declarations are invalid:

class MyClass 
{
   public void MyMethod(out int i) {i = 10;}
   public void MyMethod(ref int i) {i = 10;}
}

For information on passing an array, see Passing Arrays Using ref and out.

Example

// cs_out.cs 
using System;
public class MyClass 
{
   public static int TestOut(out char i) 
   {
      i = 'b';
      return -1;
   }

   public static void Main() 
   {
      char i;   // variable need not be initialized
      Console.WriteLine(TestOut(out i));
      Console.WriteLine(i);
   }
}

Output

-1
b

See Also

C# Keywords | Method Parameters

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft