Use of out parameter in C#

Abhisek
Posted by Abhisek under C# category on | Views : 3405
out


When out parameter is used inside the method declaration and call initialisation is not compulsory inside the main() method. It can be initialised inside the function declaration.


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


Output
r=25

Description:


In this program we are declaring a variable a inside the main method and passing it to the Change method.

d.Change(out a);

Then a receives value from i=5 inside the Change().
You have to use out in the method and also when calling the method.

Comments or Responses

Login to post response