简体   繁体   English

C# 类实例列表

[英]C# List of class instances

I am not new to programming but I am very new to C#.我对编程并不陌生,但我对 C# 很陌生。 I am trying to populate a list with instances of a class.我正在尝试使用类的实例填充列表。

I would expect the following piece of code to display numbers 0 to 9 in the console but instead it repeats 9 ten times.我希望下面的代码在控制台中显示数字 0 到 9,但它会重复 9 十次。 Clearly I am doing something wrong.显然我做错了什么。

I am suspecting that adding "a" to the list simply adds a reference to "a", and not a new instance of class1.我怀疑将“a”添加到列表中只是添加了对“a”的引用,而不是 class1 的新实例。 I am not sure what I should be adding though.我不确定我应该添加什么。 What would be the correct syntax to add a new instance of class1 to the list?将 class1 的新实例添加到列表中的正确语法是什么?

Thanks in advance for any answer!提前感谢您的任何答案!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<class1> iList = new List<class1>();
            class1 a = new class1();

            for (int i = 0; i < 10; i++)
            {

                iList.Add(a);
                iList[i].var1 = i;
            }
            for (int i = 0; i < iList.Count; i++)
            {
                System.Console.Write("var1 of " + i + ": " + iList[i].var1 + "\r\n");
            }
            Console.ReadLine();
        }
    }

    class class1
    {
        public int var1;
    }
}

You are adding the same object over and over again, and that's it's causing that all ten items are pointing to the same object.您一遍又一遍地添加相同的对象,这就是导致所有十个项目都指向同一个对象的原因。

When you add an object to a list, you are not creating a new object.将对象添加到列表时,您并不是在创建新对象。 You are adding a reference to the object you are adding.您正在添加对正在添加的对象的引用。 In this example, you are adding the same reference 10 times.在本例中,您添加了 10 次相同的引用。 Thus, every time you access the var1 property, you are re writing the same value.因此,每次访问var1属性时,都会重新写入相同的值。

Then, when you are looping over the list (that will contain 10 references) all this references are pointing to the same object.然后,当您遍历列表(将包含 10 个引用)时,所有这些引用都指向同一个对象。

The instance of class1 should be in the loop: class1 的实例应该在循环中:

for (int i = 0; i < 10; i++)
{
    class1 a = new class1();
    iList.Add(a);
    iList[i].var1 = i;
}

Which programming language you are familiar to?你熟悉哪种编程语言? In normal popular language objects from classes are referent data types.在通常的流行语言中,来自类的对象是指称数据类型。 If you add twice the same instance in a list, changing a field in that instance will reflect both elements in the list.如果在列表中添加两次相同的实例,则更改该实例中的字段将反映列表中的两个元素。

In your particular case you are adding only one instance 10 times.在您的特定情况下,您只添加了一个实例 10 次。 On the last iteration you do change the value of the field to 9 which reflects all items in the array pointing to that instance.在最后一次迭代中,您确实将字段的值更改为9 ,这反映了指向该实例的数组中的所有项目。

If you want to achieve the desired behavior, you need to add new instances of the class.如果要实现所需的行为,则需要添加该类的新实例。 Eg to create it in the loop, not outside it.例如,在循环中创建它,而不是在它之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM