C#中的struct v/s类 - 请解释一下这种行为

abh*_*bhi 7 .net c# struct class

有人可以解释一下这种行为

  class testCompile
    {
       /*
        *   Sample Code For Purpose of Illustration
        */
       struct person 
       {
           public int age;
           public string name;

       }

        static void Main(string[] args)
        {
            List<person> Listperson = new List<person>();
            person myperson = new person();

            for (int i = 1; i <= 2; i++)
            { 
                //Assignment
                myperson.age = 22+i;
                myperson.name = "Person - " + i.ToString();
                Listperson.Add(myperson);
            }
            int x = 0;
            while (x < Listperson.Count)
            {
                //Output values
                Console.WriteLine("{0} - {1}", Listperson[x].name, Listperson[x].age);
                x++;
            }
        }
    }

/*  
    Output:
    Person - 1 - 23
    Person - 2 - 24
*/
Run Code Online (Sandbox Code Playgroud)

为什么我没有获得与结构类相同的输出?

class testCompile
    {
       /*
        *   Sample Code For Purpose of Illustration
        */
       class person 
       {
           public int age;
           public string name;

       }

        static void Main(string[] args)
        {
            List<person> Listperson = new List<person>();
            person myperson = new person();

            for (int i = 1; i <= 2; i++)
            { 
                //Assignment
                myperson.age = 22+i;
                myperson.name = "Person - " + i.ToString();
                Listperson.Add(myperson);
            }
            int x = 0;
            while (x < Listperson.Count)
            {
                //Output values
                Console.WriteLine("{0} - {1}", Listperson[x].name, Listperson[x].age);
                x++;
            }
        }
    }
/*  
    Output:
    Person - 2 - 24
    Person - 2 - 24 
*/
Run Code Online (Sandbox Code Playgroud)

Ode*_*ded 17

类是引用类型,结构是类型.

类型作为参数传递给方法时,将传递它的副本.这意味着您要添加两个完全独立Person结构副本,一个用于循环中的每个传递.

引用类型作为参数传递给方法时,将传递引用.这意味着您将引用的两个副本添加到同一个内存位置(到同一个Person对象) - 当对这个对象进行更改时,您会看到它反映在两个引用中,因为它们都引用同一个对象.


Jul*_*rau 5

它是值类型(struct)和引用类型(class)之间的区别.

  • 当您将结构添加到人员Listperson的内容放入列表中时,您的列表中有两个不同的人结构.

    for (int i = 1; i <= 2; i++)
    { 
      //Assignment
      myperson.age = 22+i;
      myperson.name = "Person - " + i.ToString();
      Listperson.Add(myperson);
      /* First time: 
         Listperson contains a person struct with value { age = 23, name = 1}
         Second iteration:
         Listperson contains a person struct with value { age = 23, name = 1}
         Listperson contains another person struct with value { age = 24, name = 2} 
      */
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 当您添加类时,引用放在列表中,您有两个引用引用同一个人对象的引用.

    for (int i = 1; i <= 2; i++)
    { 
      //Assignment
      myperson.age = 22+i;
      myperson.name = "Person - " + i.ToString();
      Listperson.Add(myperson);
      /* First time: 
         Listperson contains 1 reference to myperson object with value { age = 23, name = 1}
         Second iteration:
         Listperson contains 2 reference to myperson object with value { age = 24, name = 2} 
      */
    }
    
    Run Code Online (Sandbox Code Playgroud)