7.10.08

Output Parameters in Methods:

The return values in any function will be enough for any one if only one value is needed. But in case a function is required to return more than one value, then output parameters are the norm. This is not supported in C++ though it can be achieved by using some programming tricks. In C# the output parameter is declared with the keyword out before the data type. A typical example is as follows.

public void CalculateBirthYear(ref int year, out int birthyear)
{
int b = year - m_old;
Console.WriteLine("Birth year is {0}",b);
birthyear = b;
return;
}

Strictly speaking there is no difference between ref and out parameters.
The only difference is that the ref input parameters need an input value and the out parameters dont. Ref parameter Should be initilized before call. Out need not to initilized before call.
Ref paramenter is nothing but in & out parameter.
Out is only out parameter.
There is no performance issue occured.
A ref or out argument must be an lvalue.ie, you need to pass the same signature.
The casting is not allowed for both when you pass thevalues to ref/out.

No comments: