简体   繁体   English

手动填写C#数据类型集合列表

[英]Manually fill C# List of collection of datatypes

I am working on a C# example to learning the Lists and how to initialize them. 我正在研究一个C#示例,以学习列表以及如何初始化它们。 So I have a classes with some properties: 所以我有一些属性的类:

public class C1
{
    public string p1 { get; set; }
    public string p2 { get; set; }
    public List<StatusChoices> ChoiceList { get; set; }

    ...

    public string FillList(string v1, int v2, bool v3)
    {
        // How can I fill this.ChoiceList?

        this.ChoiceList.val1 = v1; //Is this possible?

        return this.ChoiceList.val1;
    }
}

public class StatusChoices
{
    public string val1 { get; set; }
    public int val2    { get; set; }
    public bool val3   { get; set; }
}

This is probably easy, but I wasn't able to achieve it so far. 这可能很容易,但是到目前为止我还无法实现。 How can I add some values " manually " to the list? 我怎样才能在列表中“ 手动 ”添加一些值?

The ChoiceList is initialized to null when the class C1 is created. 创建类C1时, ChoiceList初始化为null (In fact every class field, including hidden property backing fields, is initialized to its default value.) You must create the list object explicitly. (实际上,每个类字段(包括隐藏的属性支持字段)都被初始化为其默认值。)您必须显式创建列表对象。

You can do so in the class constructor 您可以在类构造函数中执行此操作

public class C1
{
    ...
    public List<StatusChoices> ChoiceList { get; set; }

    public C1() // Constructor. Is run when a new C1 object is created with `new`.
    {
        ChoiceList = new List<StatusChoices>();
    }

    ...
}

After creation, this list is empty. 创建后,此列表为空。 You must add elements to it: 您必须向其中添加元素:

public string FillList(string v1, int v2, bool v3){
    ChoiceList.Add(new StatusChoices{ val1 = v1, val2 = v2, val3 = v3 });
    return v1;
}

Note that you have to create a StatusChoices object as well. 请注意,您还必须创建一个StatusChoices对象。 You can think of a class as being a template that can be used to create objects. 您可以将类视为可用于创建对象的模板。

When you have added objects to the list, you can access them by enumeration 将对象添加到列表后,可以通过枚举访问它们

foreach (StatusChoices sc in ChoiceList) {
    Console.WriteLine($"val 1 = {sc.val1}, val 2 = {sc.val2}, val 3 = {sc.val3}")
}

or through indexing 或通过索引

StatusChoices first = ChoiceList[0];
StatusChoices second = ChoiceList[1];
string v1_of_first  = first.val1;
string v1_of_second  = second.val1;

You can also access properties of the elements directly 您还可以直接访问元素的属性

string v1_of_first  = ChoiceList[0].val1;
string v1_of_second  = ChoiceList[1].val1;

If you have an object C1 c1 = new C1(); 如果您有一个对象C1 c1 = new C1(); , you can write , 你可以写

string v = c1.ChoiceList[0].val1;

You could even construct a list of C1 objects 您甚至可以构造一个C1对象列表

var mainList = new List<C1>(); // The var-keyword lets C# infer the type.
                               // This saves you from writing List<C1> again.
mainList.Add(new C1());
mainList.Add(new C1());
mainList.Add(new C1());
mainList[0].FillList("a", 12, true);
mainList[0].FillList("b", 33, false);
mainList[1].FillList("x", 77, true);
...
string v = mainList[1].ChoiceList[2].val1;

I used ChoiceList.Add(new StatusChoices{ val1 = v1, val2 = v2, val3 = v3 }); 我使用ChoiceList.Add(new StatusChoices{ val1 = v1, val2 = v2, val3 = v3 }); to add a new element to the list. 将新元素添加到列表中。 This is a short version of 这是

StatusChoices sc = new StatusChoices();
sc.val1 = v1;
sc.val2 = v2;
sc.val3 = v3;
ChoiceList.Add(sc);

The short version uses an object initializer { val1 = v1, ... } . 简短版本使用对象初始化程序{ val1 = v1, ... }

Something like this 像这样

public string FillList(string v1, int v2, bool v3){
    this.ChoiceList = new List<StatusChoices> {
     new StatusChoices { val1 = v1, val2 = v2, val3 = v3, } };
    return this.ChoiceList[0].val1;
}

Simply create an instance of the StatusChoices class, initialize it and add it to the list. 只需创建StatusChoices类的实例,对其进行初始化并将其添加到列表中。

(I added lazy initialization of the List) (我添加了列表的惰性初始化)

public class C1{
    public string p1 {get; set; }
    public string p2 {get; set; }
    public List<StatusChoices> ChoiceList { get; set; }
    ...
    public string FillList(string v1, int v2, bool v3){

        if(ChoiceList == null)
        {
            this.ChoiceList = new List<StatusChoices>();
        }

        var newItem = new StatusChoices {val1 = v1, val2 = v2, val3 = v3};
        this.ChoiceList.Add(newItem);

        return newItem.val1;
    }
}

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

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