I was trying to understand difference between call by value and call by reference. Someone explained me that by reference changes actual memory location value, whereas the call by value just changes the instance(virtual value) but not actual one. So I tried to make that program check how by value and by reference work. My program is not working, Am i implementing logic correctly?
Here is the revised version of solved and working code.
static void Main(string[] args)
{
Program pro = new Program();
int i = 1;
Console.WriteLine("Call By Value: ");
pro.byVal(i);
Console.WriteLine(i);
Console.ReadKey();
Console.WriteLine("\n\n\nCall By Reference: ");
pro.byRef(ref i);
Console.WriteLine(i);
Console.ReadKey();
}
public void byVal(int i) //******* Call by Value *******//
{
i = 2;
}
public void byRef(ref int i) //******* Call by Refrence *******//
{
i = 3;
}
You need to change the call
byRef(i);
to
byRef(ref i);
if you want to call it by reference.