如何插入值到C#字典上的实例? [英] How to insert values into C# Dictionary on instantiation?
本文介绍了如何插入值到C#字典上的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道,如果有一种方法,当我创建它,我可以将值插入一个C#字典?我可以,但不想,做
dict.Add(INT字符串)
为每个项目,如果有什么更有效,如:
词典< INT,串>(){(0,串),(1,字符串2),(2 STRING3)};
解决方案
有是关于如何做到这一点在这里整个页面:
http://msdn.microsoft.com/ EN-US /库/ bb531208.aspx
例如:
在下面的代码示例中,
词典< TKEY的,TValue>
是
与类型的实例初始化StudentName
:
块引用>
VAR学生=新词典< INT,StudentName>()
{
{111,新StudentName {名字=萨钦,姓氏=卡尼克,ID = 211}},
{112,新StudentName {名字=迪娜,姓氏=Salimzianova ID = 317}}
{113,新StudentName {名字=安迪,姓氏=露丝,ID = 198}}
};
Does anyone know if there is a way I can insert values into a C# Dictionary when I create it? I can, but don't want to, do
dict.Add(int, "string")
for each item if there is something more efficient like:Dictionary<int, string>(){(0, "string"),(1,"string2"),(2,"string3")};
解决方案There's whole page about how to do that here:
http://msdn.microsoft.com/en-us/library/bb531208.aspx
Example:
In the following code example, a
Dictionary<TKey, TValue>
is initialized with instances of typeStudentName
:
var students = new Dictionary<int, StudentName>() { { 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}}, { 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}}, { 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}} };
这篇关于如何插入值到C#字典上的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文