Thursday 22 March 2012

Method Arguments and Parameters


using System;
class ParamTest
{
//call by value
public  void Update(int a)
{
a=a+10;
}

//call by Reference
public  void Swap(ref int a,ref int b)
{
int t=a;
a=b;
b=t;
}

//call by Out
public  void Power(int a,out int b,out int c)
{
b=a*a;
c=a*a*a;
}
}

class Program
{
public static void Main()
{
ParamTest obj=new ParamTest();

int x=10,a=10,b=20,y,z;
Console.WriteLine("Before Update x={0}",x);
obj.Update(x);
Console.WriteLine("After Update x={0}",x);

Console.WriteLine("Before Swapping a={0} and b={1}",a,b);
obj.Swap(ref a,ref b);
Console.WriteLine("Before Swapping a={0} and b={1}",a,b);

x=5;
obj.Power(x,out y,out z);
Console.WriteLine("X:{0}, Y={1} Z={2}",x,y,z);
}
}

No comments:

Post a Comment