在 C# 中将数组添加到字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9526626/
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
Add an Array to a Dictionary in C#
提问by miltonjbradley
I have tried reading the other posts on this subject and can't quite figure this out.
我曾尝试阅读有关此主题的其他帖子,但无法弄清楚这一点。
I have a list in C# that I want to put in a dictionary with all of the same keys. The list is this
我在 C# 中有一个列表,我想将它放入具有所有相同键的字典中。清单是这个
string[] IN ={"Against","Like","Upon","Through","Of","With","Upon","On","Into","From","by","that","In","About","For"
,"Along","Before","Beneath","At","Across","beside","After","Though","Among","Toward","If"};
I want to create and populate a dictionary with the key being "IN" (the name of the array) and then having each string for the array in the dictionary.
我想创建并填充一个字典,键是“IN”(数组的名称),然后在字典中包含数组的每个字符串。
This is what I wrote to create the dictionary (which I am not sure is correct):
这是我为创建字典而写的内容(我不确定它是否正确):
Dictionary<string, List<string>> wordDictionary = new Dictionary<string, List<string>> ()
But I am not sure how to populate the dictionary.
但我不确定如何填充字典。
Any help would be greatly appreciated as this is the first time I have tried to use a dictionary and I am new to C#
任何帮助将不胜感激,因为这是我第一次尝试使用字典,而且我是 C# 新手
采纳答案by Ry-
An array is string[], not List<string>, so just do this:
一个数组是string[],不是List<string>,所以只需这样做:
Dictionary<string, string[]> wordDictionary = new Dictionary<string, string[]>();
Now you can add your array as usual.
现在您可以像往常一样添加数组。
wordDictionary.Add("IN", IN);
Or:
或者:
wordDictionary.Add("IN", new string[] {"Against","Like","Upon","Through","Of","With","Upon","On","Into","From","by","that","In","About","For","Along","Before","Beneath","At","Across","beside","After","Though","Among","Toward","If"});
回答by BrokenGlass
You currently have a string array, not a list - so it should be:
您目前有一个字符串数组,而不是列表 - 所以它应该是:
Dictionary<string, string[]> wordDictionary = new Dictionary<string,string[]> ()
Then you can just add items like:
然后您可以添加以下项目:
wordDictionary.Add("IN" , IN);
回答by Robert Harvey
Dictionary.Add("IN", new List<string>(IN));
...if you want to keep the current signature for your dictionary.
...如果您想保留字典的当前签名。
If you change it to Dictionary<string, string[]>then you can just:
如果你改变它,Dictionary<string, string[]>那么你可以:
Dictionary.Add("IN",IN);
回答by rikitikitik
Do you really need to convert your array into a string? You could very well use string[] instead of List in your dictionary:
您真的需要将数组转换为字符串吗?您可以在字典中使用 string[] 而不是 List :
var wordDictionary = new Dictionary<string, string[]>();
wordDictionary.Add("IN", IN);
But if you really want to convert your string array to List:
但是,如果您真的想将字符串数组转换为 List:
var wordDictionary = new Dictionary<string, List<string>>();
wordDictionary.Add("IN", IN.ToList());
回答by svick
Another way to add the array (it's not a list) to the dictionary is to use collection initializer:
将数组(它不是列表)添加到字典的另一种方法是使用集合初始值设定项:
var wordDictionary = new Dictionary<string, string[]> { "IN", IN };
This is exactlythe same as creating the dictionary in a normal way and then calling Add("IN", IN).
这正好与在正常方式创建字典,然后调用Add("IN", IN)。

