Monday, March 1, 2010

Ref and out parameters

ref= in and out. Both allow the member functions to change the values of the parameters. So the difference between ref and out is ,for out, the variable passed as the parameter doesn’t have to have a known value before the call.

Here are an example:

static void Main()
{
string value1 = "cat";
SetString1(ref value1);
Console.WriteLine(value1);

string value2; // Unassigned string
SetString2(out value2);
Console.WriteLine(value2);
}

static void SetString1(ref string value)
{
value = "dog";
}
static void SetString1(out string value)
{
value = "bird";
}

Output:
dog
bird

No comments: