Use of ref parameter in C#

Abhisek
Posted by Abhisek under C# category on | Views : 2690
Ref Parameter:



The ref keyword is used to pass the reference of a variable to a function. It is used in call by reference.

Any changes made to the formal parameter affect the subroutine during method call.

The ref keyword is used both in formal and actual parameter.


class Demo
{
public int Change(ref int i)
{
return i * i;
}
}
class Change
{
static void main(string a[])
{
Demo d=new Demo();
int a=5;
int r= d.Change(ref a);
Console.WriteLine("r="+r);
}
}



Output:
r=25

Description:


In this case the value of a has changed from 5 to 25. We can't pass the value inside the method. We have to pass the value inside the main().

Comments or Responses

Login to post response