Sunday, October 3, 2010

Understanding the Parameters passing in C #.

There are four kinds of passing parameters to a method in C# which are as:.

a) Value parameters.

b) Reference parameters.

c) Output parameters.

d) Parameter arrays.


a) Value parameters: - A value parameters is used for “in” parameter passing, in which the value of an argument is passed into a method, and modifications of the parameter do not impact the original argument. A value parameter refers to its own variable, one that is distinct from the corresponding argument. This variable is initialized by copying the value of the corresponding argument.


Example:

using System;

namespace value_parameter
{

class Test

{

class Test1

{

public int sum(int a, int b)

{

a = a + 20;

b = b + 40;

return (a + b);

}

}
static void Main(string[] args)

{

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

int a = 20, b = 40;

Test1 obj = new Test1();

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

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

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

Console.ReadLine();

}

}

}


Note:-In the above code we changed the values of data member a and b but it is not reflected back in the calling method.




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

No comments:

Recent Posts