如何初始化字典列表
本文关键字:列表 字典 初始化 | 更新日期: 2023-09-27 18:21:38
如何使用集合初始化器初始化System.Collections.Generic.Dictionary
对象的System.Collections.Generic.List
?
List<Dictionary<string, object>> listaDeDicionarios = new List<Dictionary<string, object>>()
{
new Dictionary<string,object>()
{
{"chave1","valor um"},
{"chave2","dois"},
{"chave3",true}
},
new Dictionary<string,object>()
{
{"chave4",4},
{"chave5",DateTime.Now},
{"chave6",long.MaxValue},
{"chave7","..."}
},
new Dictionary<string,object>()
{
{"chave8","utf string áéí / &url"},
{"chave9",null},
{"chave10",String.Empty},
{"chave11",new List<string>(){"aaa","bbb","ccc","oh my god, it's a miracle!"}}
}
};
如果您使用C#6.0,您可以使用:
var myDict = new Dictionary<int, string>
{
[1] = "Pankaj",
[2] = "Pankaj",
[3] = "Pankaj"
};