1. using System;
    2. namespace 交换变量
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. //交换变量,使用第三方变量
    9. int a = 1;
    10. int b = 2;
    11. Console.WriteLine("a的值为{0},b的值为{1}", a, b);
    12. int temp = a;
    13. a = b;
    14. b = temp;
    15. Console.WriteLine("现在a的值为{0},b的值为{1}",a,b);
    16. //不使用第三方变量
    17. // 第一种:
    18. int m = 5;
    19. int n = 3;
    20. Console.WriteLine("\nc的值为{0},d的值为{1}", m, n);
    21. m = m - n;//此时m为两者的差
    22. n = m + n;//此时n为初始m的值
    23. m = n - m;//此时m为初始n的值
    24. Console.WriteLine("现在a的值为{0},b的值为{1}",m, n);
    25. // 第二种:
    26. int n1 = 100;
    27. int n2 = 10;
    28. Console.WriteLine("\nn1的值为{0},n2的值为{1}", n1, n2);
    29. n1 = n1 + n2;
    30. n2 = n1 - n2;
    31. n1 = n1 - n2;
    32. Console.WriteLine("现在n1的值为{0},n2的值为{1}", n1, n2);
    33. }
    34. }
    35. }

    交换int,string,char,double都用第一种最简单