Thursday, August 19, 2010

Output Parameters in C#

Output Parameter


A function can return only a single value. But when we using output parameters it is possible to return multiple values.
Note: output parameters must be assigned with value before leaving method, otherwise it will results in a compilation error.
Eg

using System;
class Test
{


public void Cal(int a, int b, out int c, out int d) // a and b are input parameters. c and d are output parameters.
{
c = a + b;   // set value for output parameter c.
d = a - b;   // set value for output parameter d.}


}

class Mainclass
{
public static void Main()
{
int n1 = 10, n2 = 30, sum, dif;
Test obj = new Test();
obj.Cal(n1, n2, out sum, out dif);
Console.WriteLine("Sum is {0} and Difference is {1} ", sum, dif);
}
}

No comments: