What is the difference between ref and out parameters?

Ref parameter

Quote:

1: An argument passed to a ref parameter must be first initialized

The ref keyword on a method parameter causes a method to refer to the same variable that was passed as an input parameter for the same method. If you do any changes to the variable, they will be reflected in the variable.

You can even use ref for more than one method parameters.

Code:

namespace RefPerameterClass
{ 
using System; 
public class RefClass 
{ 
public static void CalculateRefPerameter(ref int intRefA ) 
{ 
intRefA += 2; 
} 
public static void Main() 
{ 
int i; // variable need to be initialized 
i = 3; 
CalculateRefPerameter (ref i ); 
Console.WriteLine(i); 
} 
} 
}

Out parameter

Quote:

1: An argument passed to an output parameter does not need to be explicitly initialized.

The out parameter can be used to return the values in the same variable passed as a parameter of the method. Any changes made to the parameter will be reflected in the variable.

Code:

public class OutPerameterClass
{
public static int CalculateOutPerameter(out int intOutA, out int intOutB)
{
intOutA = 10;
intOutB = 20;
return 0;
} 
public static void Main()
{
int i, j; // variable need not be initialized
Console.WriteLine(CalculateOutPerameter (out i, out j));
Console.WriteLine(i);
Console.WriteLine(j);
}
}