第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

手動(dòng)填充 C# 數(shù)據(jù)類型集合列表

手動(dòng)填充 C# 數(shù)據(jù)類型集合列表

C#
慕的地6264312 2021-10-09 10:45:51
我正在編寫一個(gè) C# 示例來學(xué)習(xí)列表以及如何初始化它們。所以我有一個(gè)具有一些屬性的類: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; }}這可能很容易,但到目前為止我無(wú)法實(shí)現(xiàn)。如何“手動(dòng)”將一些值添加到列表中?
查看完整描述

3 回答

?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

將ChoiceList被初始化為null類時(shí)C1被創(chuàng)建。(事實(shí)上,每個(gè)類字段,包括隱藏的屬性支持字段,都被初始化為其默認(rèn)值。)您必須顯式創(chuàng)建列表對(duì)象。


您可以在類構(gòu)造函數(shù)中這樣做


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>();

    }


    ...

}

創(chuàng)建后,此列表為空。您必須向其中添加元素:


public string FillList(string v1, int v2, bool v3){

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

    return v1;

}

請(qǐng)注意,您還必須創(chuàng)建一個(gè)StatusChoices對(duì)象。您可以將類視為可用于創(chuàng)建對(duì)象的模板。


將對(duì)象添加到列表后,可以通過枚舉訪問它們


foreach (StatusChoices sc in ChoiceList) {

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

}

或通過索引


StatusChoices first = ChoiceList[0];

StatusChoices second = ChoiceList[1];

string v1_of_first  = first.val1;

string v1_of_second  = second.val1;

您還可以直接訪問元素的屬性


string v1_of_first  = ChoiceList[0].val1;

string v1_of_second  = ChoiceList[1].val1;

如果你有一個(gè)對(duì)象C1 c1 = new C1();,你可以寫


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

你甚至可以構(gòu)造一個(gè)C1對(duì)象列表


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;

我曾經(jīng)ChoiceList.Add(new StatusChoices{ val1 = v1, val2 = v2, val3 = v3 });在列表中添加一個(gè)新元素。這是一個(gè)簡(jiǎn)短的版本


StatusChoices sc = new StatusChoices();

sc.val1 = v1;

sc.val2 = v2;

sc.val3 = v3;

ChoiceList.Add(sc);

簡(jiǎn)短版本使用對(duì)象初始值設(shè)定項(xiàng){ val1 = v1, ... }。


查看完整回答
反對(duì) 回復(fù) 2021-10-09
?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

像這樣的東西


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;

}


查看完整回答
反對(duì) 回復(fù) 2021-10-09
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊

只需創(chuàng)建 StatusChoices 類的一個(gè)實(shí)例,對(duì)其進(jìn)行初始化并將其添加到列表中。


(我添加了列表的延遲初始化)


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;

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-10-09
  • 3 回答
  • 0 關(guān)注
  • 221 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)