internal class Program
{
private static void Main(string[] args)
{
int a = 1, b = 2;
int x = 3, y = 4;
Swap(a,b);
RefSwap(ref x,ref y);
Console.WriteLine("a:{0} b:{1}", a, b);
Console.WriteLine("x:{0} y:{1}", x, y);
Console.ReadKey();
}
static void Swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
static void RefSwap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
}
a和b是按值传递 函数交换的是a和b的副本 原来的a和b没有交换
x和y是按引用传递 交换了
|