Monday, October 4, 2010

Understanding the output Parameter in C #.

Output parameter: -
An output parameter is similar to a reference parameter, except that the initial value of the caller-provided argument is unimportant. An output parameter is declared with an out modifier.

The example:

using System;

namespace out_parameter
{

class Test
{

class Test1
{

public int sum(int a, int b, out int c, out int d)
{

c = a + b;

d = c + 100;

return (a + b);

}

}

static void Main(string[] args)
{

// local data members have to initialized as they are not initiated with class constructor

int a = 50, b = 60;

// the out data members doesn't need to assign initial value as they are processed and brought back

int c, d;

Test1 obj = new Test1();

Console.WriteLine("sum of a and b is : " + obj.sum(a, b, out c, out d));

Console.WriteLine("Value of a is : " + a);

Console.WriteLine("Value of b is : " + b);

Console.WriteLine("Value of c is : " + c);

Console.WriteLine("Value of d is : " + d);

Console.ReadLine();

}

}

}


The output is display below.
sum of a and b is : 110
Value of a is : 50
Value of b is : 60
Value of c is : 110
Value of d is : 210



What Other's are reading.
Understanding the passing value Parameter in C #.
Understanding the Reference Parameter in C #.
Understanding the Parameter array in C #.

No comments:

Recent Posts