环境:net framework 4.5.2


    struct是值传递(拷贝)

    class 和 struct的区别 - 图1

    class 和 struct的区别 - 图2

    class 和 struct的区别 - 图3

    class 和 struct的区别 - 图4

    1. public class Test1 {
    2. public Test1(int x,int y){
    3. this.X = x;
    4. this.Y = y;
    5. }
    6. public int X {get;set;}
    7. public int Y {get;set;}
    8. }
    9. public struct Test2 {
    10. public Test2(int x,int y){
    11. this.X = x;
    12. this.Y = y;
    13. }
    14. public int X {get;set;}
    15. public int Y {get;set;}
    16. }
    17. public class Program {
    18. private void EditTest(Test1 T) {
    19. T.X = 500;
    20. T.Y = 1000;
    21. }
    22. private void EditTest(Test2 T) {
    23. T.X = 5000;
    24. T.Y = 10000;
    25. }
    26. static void Main(string [] args) {
    27. Test1 t1 = new Test1(10,20);
    28. Test2 t2 = new Test2(30,40);
    29. Program pro = new Program();
    30. pro.EditTest(t1);
    31. pro.EditTest(t2);
    32. Console.WriteLine("x = {0},y = {1}",t1.X,t1.Y); //X = 500, Y = 1000
    33. Console.WriteLine("x = {0},y = {1}", t2.X, t2.Y);//X = 30, Y = 40
    34. }
    35. }