Thursday, August 19, 2010

Reference Parameters in C#

Reference Parameter


These are input/output parameters; they can be used for passing value into a function as well as to get back value from a function.

using System;
class Test
{


public void Inc(ref int a) // here a is a reference parameter. It refer the same memory address of passing arguement.
{
a = a + 10;
Console.WriteLine(a);
}
}



class Mainclass
{
public static void Main()
{
int x = 10;
Test obj = new Test();
obj.Inc(ref x); // arguement x and parameter a will be using the same memory reference.
Console.WriteLine("From Main " + x);
}
}

No comments: