结构V / S级在C#中 - 请解释行为行为、结构

2023-09-02 21:47:01 作者:独年荒岛-ALONG

可能有人请解释这种行为

 类testCompile
    {
       / *
        *样品code对于说明的目的
        * /
       结构人
       {
           公众诠释年龄;
           公共字符串名称;

       }

        静态无效的主要(字串[] args)
        {
            名单<人> Listperson =新的名单,其中,人>();
            人myperson =新的人();

            的for(int i = 1; I< = 2;我++)
            {
                //分配
                myperson.age = 22 + I;
                myperson.name =人 - + i.ToString();
                Listperson.Add(myperson);
            }
            INT X = 0;
            而(X< Listperson.Count)
            {
                //输出值
                Console.WriteLine({0}  -  {1},Listperson [X] .name和Listperson [X]。年龄);
                X ++;
            }
        }
    }

/ *
    输出:
    人 -  1  -  23
    人 -  2  -  24
* /
 

为什么我不能得到相同的输出的一类作为一个结构呢?

 类testCompile
    {
       / *
        *样品code对于说明的目的
        * /
       类人
       {
           公众诠释年龄;
           公共字符串名称;

       }

        静态无效的主要(字串[] args)
        {
            名单<人> Listperson =新的名单,其中,人>();
            人myperson =新的人();

            的for(int i = 1; I< = 2;我++)
            {
                //分配
                myperson.age = 22 + I;
                myperson.name =人 - + i.ToString();
                Listperson.Add(myperson);
            }
            INT X = 0;
            而(X< Listperson.Count)
            {
                //输出值
                Console.WriteLine({0}  -  {1},Listperson [X] .name和Listperson [X]。年龄);
                X ++;
            }
        }
    }
/ *
    输出:
    人 -  2  -  24
    人 -  2  -  24
* /
 

解决方案

类是的引用的类型,结构是值的类型。

电子科大熊杰团队Angew.精彩综述 纳米材料的电子结构调变与高效水裂解

在值的类型传递给方法作为一个参数,一个复制它会被通过。这意味着,您添加结构,每一个传中循环。两个完全独立的副本

在引用的类型传递给方法作为参数,参考将被通过。这意味着,您添加引用的两个复制到同一个内存位置(同一个)对象 - 在更改此一个对象时, ,你看它体现在两个引用,因为它们都引用同一个对象。

Could someone please explain the behavior of this

  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
*/

Why am I not getting the same output for a class as that of a struct?

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 
*/

解决方案

Classes are reference types, structs are value types.

When a value type is passed to a method as a parameter, a copy of it will be passed through. That means that you add two completely separate copies of the Person struct, one for each pass in the loop.

When a reference type is passed to a method as a parameter, the reference will be passed through. That mean that you add two copies of the reference to the same memory location (to the same Person object) - when making changes to this one object, you see it reflected in both references since they both reference the same object.