不使用第三个变量,实现两个变量值的交换
static void Main(string[] args)
{
int a = 4, b = 5;
Console.WriteLine("交换前:");
Console.WriteLine("a=" + a);
Console.WriteLine("b=" + b);
SwapAB(ref a, ref b);
Console.WriteLine("交换后:");
Console.WriteLine("a=" + a);
Console.WriteLine("b=" + b);
Console.ReadLine();
}
private static void SwapAB(ref int a, ref int b)
{
a = a + b - (b = a);
}
更多前端内容请访问个人博客:殷路辉的个人博客