如何在实例化时将值插入到 C# 字典中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1039610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert values into C# Dictionary on instantiation?
提问by kd7iwp
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:
有谁知道我在创建 C# 字典时是否可以将值插入它?dict.Add(int, "string")
如果有更有效的东西,我可以,但不想为每个项目做
:
Dictionary<int, string>(){(0, "string"),(1,"string2"),(2,"string3")};
采纳答案by Daniel Earwicker
There's whole page about how to do that here:
这里有整页关于如何做到这一点:
http://msdn.microsoft.com/en-us/library/bb531208.aspx
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
:
在以下代码示例中, a
Dictionary<TKey, TValue>
使用类型的实例进行初始化StudentName
:
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}}
};
回答by Timothy Carter
Dictionary<int, string> dictionary = new Dictionary<int, string> {
{ 0, "string" },
{ 1, "string2" },
{ 2, "string3" } };
回答by Joseph
You can instantiate a dictionary and add items into it like this:
您可以实例化字典并向其中添加项目,如下所示:
var dictionary = new Dictionary<int, string>
{
{0, "string"},
{1, "string2"},
{2, "string3"}
};
回答by Henk Holterman
You were almost there:
你快到了:
var dict = new Dictionary<int, string>()
{ {0, "string"}, {1,"string2"},{2,"string3"}};
回答by matendie
You can also use Lambda expressions to insert any Key Value pairs from any other IEnumerable object. Key and value can be any type you want.
您还可以使用 Lambda 表达式从任何其他 IEnumerable 对象插入任何键值对。键和值可以是您想要的任何类型。
Dictionary<int, string> newDictionary =
SomeList.ToDictionary(k => k.ID, v => v.Name);
I find that much simpler since you use the IEnumerable objects everywhere in .NET
我发现这更简单,因为您在 .NET 中随处使用 IEnumerable 对象
Hope that helps!!!
希望有帮助!!!
Tad.
泰德。
回答by matendie
Hope it will work perfectly.
希望它能完美运行。
Dictionary<string, double> D =new Dictionary<string, double>();
D.Add("String", 17.00);
Dictionary<string, double> D =new Dictionary<string, double>();
D.Add("String", 17.00);
回答by russelrillema
Just so you know as of C# 6 you can now initialize it as follows
为了让您知道从 C# 6 开始,您现在可以按如下方式初始化它
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}
};
Much cleaner :)
干净多了:)
回答by Hymansonkr
This isn't generally recommended but in times of uncertain crises you can use
通常不建议这样做,但在不确定的危机时期您可以使用
Dictionary<string, object> jsonMock = new Dictionary<string, object>() { { "object a", objA }, { "object b", objB } };
// example of unserializing
ClassForObjectA anotherObjA = null;
if(jsonMock.Contains("object a")) {
anotherObjA = (ClassForObjA)jsonMock["object a"];
}