Hi All. i want Swap two value type like int but i can't understand difference between these two method. that on uses from 'Ref' keyword but second doesn't use ref
so What is different between methods?
1. Swap method with ref keyword
static void Main()
{
int i, j;
i = 1;
j = 2;
Console.WriteLine(" Befor Swap:\n" + "i:{0}\tj:{1}", i, j);
Swap(ref i, ref j);
Console.WriteLine(" After Swap:\n" + "i:{0}\tj:{1}", i, j);
Console.ReadLine();
}
public static void Swap(ref int a, ref int b)
{
int t;
t = a;
a = b;
b = t;
}
2.Swap Method without ref keyword
static void Main()
{
int i, j;
i = 1;
j = 2;
Console.WriteLine(" Befor Swap:\n" + "i:{0}\tj:{1}", i, j);
Swap(i, j);
Console.WriteLine(" After Swap:\n" + "i:{0}\tj:{1}", i, j);
Console.ReadLine();
}
static void Swap(int a, int b)
{
int t;
t = a;
a = b;
b = t;
}
::: So . Why do we use ref keyword in this case??