不使用第三个变量,实现两个变量值的交换

 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);
        }

posted @ 2012-07-09 18:14  Alec-Yin  阅读(301)  评论(0编辑  收藏  举报
更多前端博客,访问:http://lhyin.com