mil*_*ley 6 c# arrays dictionary
我曾尝试阅读有关此主题的其他帖子,但无法弄清楚这一点.
我在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"};
Run Code Online (Sandbox Code Playgroud)
我想创建并填充一个字典,其中键为"IN"(数组的名称),然后在字典中为数组提供每个字符串.
这是我写的创建字典(我不确定是否正确):
Dictionary<string, List<string>> wordDictionary = new Dictionary<string, List<string>> ()
Run Code Online (Sandbox Code Playgroud)
但我不知道如何填写字典.
任何帮助将不胜感激,因为这是我第一次尝试使用字典而且我是C#的新手
Ry-*_*Ry- 15
数组string[]
不是List<string>
,所以只需这样做:
Dictionary<string, string[]> wordDictionary = new Dictionary<string, string[]>();
Run Code Online (Sandbox Code Playgroud)
现在您可以照常添加数组.
wordDictionary.Add("IN", IN);
Run Code Online (Sandbox Code Playgroud)
要么:
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"});
Run Code Online (Sandbox Code Playgroud)