简体   繁体   English

C#对象列表

[英]C# List of Objects

I'm not sure how to create a list of objects. 我不确定如何创建对象列表。 I get "Non-invocable member ListObjects.TestObject.UniqueID' cannot be used like a method." 我收到“不能像方法一样使用不可发言的成员ListObjects.TestObject.UniqueID'”。

Any insight would be helpful. 任何见解都会有所帮助。

The code for the object I'm trying to create a list of: 我正在尝试创建以下对象的对象的代码:

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

namespace ListObjects
{
    class TestObject
    {
        public int UniqueID { get; set; }


    }
}

Main code: 主要代码:

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

namespace ListObjects
{
    class Program
    {
        static void Main(string[] args)
        {
            TestObject TestObject = new TestObject();
            List<TestObject> ObjectList = new List<TestObject>();

            ObjectList.Add(new TestObject().UniqueID(1));
            ObjectList.Add(new TestObject().UniqueID(10));
            ObjectList.Add(new TestObject().UniqueID(39));

        }
    }
}

public int UniqueID { get; public int UniqueID {get; set; 组; } is not a method its a setter and you use it like a method }不是它的设置方法,您像方法一样使用它

do this 做这个

ObjectList.Add(new TestObject()
{
    UniqueID = 1
});

I cannot rememer the syntax but it is from memory: 我无法修改语法,但是它来自内存:

repalce this line: 替换此行:

ObjectList.Add(new TestObject().UniqueID(1));

with this line: 用这一行:

ObjectList.Add(new TestObject(){UniqueID = 1});

and do the same for all .Add lines you have. 并对所有.Add行进行相同的操作。

You are using UniqueId like a method. 您正在像方法一样使用UniqueId。 It is a property and must be assigned. 它是一个属性,必须进行分配。 If you know you'll be assigning an ID when creating a TestObject, you should make the constructor support that, and use it accordingly. 如果您知道在创建TestObject时将分配一个ID,则应使构造函数支持该ID,并相应地使用它。 If not, use ObjectList.Add(new TestObject { UniqueID = 1 }); 如果不是,请使用ObjectList.Add(new TestObject { UniqueID = 1 }); to assign the value without a constructor. 在没有构造函数的情况下分配值。

This is how I would handle the situation: 这就是我要处理的情况:

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

namespace ListObjects
{
    class TestObject
    {
        public int UniqueID { get; set; }

        public TestObject(int uniqueId)
        {
            UniqueID = uniqueId;    
        }
    }
}

Main code: 主要代码:

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

namespace ListObjects
{
    class Program
    {
        static void Main(string[] args)
        {
            List<TestObject> ObjectList = new List<TestObject>();

            ObjectList.Add(new TestObject(1));
            ObjectList.Add(new TestObject(10));
            ObjectList.Add(new TestObject(39));

        }
    }
}

There's a couple of things going on here. 这里发生了几件事。 You don't need an instance of the object to declare the list, just the type. 您不需要对象的实例来声明列表,只需声明类型。

Also, UniqueID is a property, not a method. 另外, UniqueID是属性,而不是方法。 You assign values to them (or read values from them), you don't pass in values like a parameter for a method. 您为它们分配值(或从中读取值),而不会像方法的参数那样传递值。

You can also initialize the list in one call, like this: 您也可以在一个调用中初始化列表,如下所示:

List<TestObject> ObjectList = new List<TestObject>() 
                 { new TestObject() { UniqueID = 1}, 
                   new TestObject() { UniqueID = 10 },
                   new TestObject() { UniqueID = 39 } };

This will result in a new List<T> where T is of type TestObject , and the list is initialized to 3 instances of TestObject , with each instance initialized with a value for UniqueID . 这将导致一个新的List<T> ,其中T的类型为TestObject ,并且该列表初始化为3个TestObject实例,每个实例均使用UniqueID的值初始化。

 TestObject TestObject = new TestObject();

Remove this line. 删除此行。

And edit following lines to this syntax 并编辑此语法的以下几行

new TestObject { UniqueID = 39 }

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

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