Answer: Out keyword is used for passing a variable for output purpose. It has the same concept of ref keyword. Passing a ref parameter needs variable to be initialized while out parameter is passed without initialized.
It is useful when we want to return more than one value from the method.
Example:
class Test
{
public static void Main()
{
int a; // may be left un-initialized
DoWork(out a); // note out
Console.WriteLine("The value of a is " + a);
}
public static void DoWork(out int i) // note out
{
i=4; //must assigned value.
}
}
The program will result : The value of a is 4
Found interesting? Add this to: