在C#中添加一个数组到一个字典 [英] Add an Array to a Dictionary in C#

查看:576
本文介绍了在C#中添加一个数组到一个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试阅读这个主题的其他帖子,并不能完全弄清楚这一点。

I have tried reading the other posts on this subject and can't quite figure this out.

我有一个C#中的列表,我想放入一个字典与所有相同的键。列表是这个

I have a list in C# that I want to put in a dictionary with all of the same keys. The list is this

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"};

我想使用IN键创建并填充字典(数组名称),然后在字典中有数组的每个字符串。

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.

这是我写的创建字典(我不知道是否正确):

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.

任何帮助将不胜感激,因为这是我第一次尝试使用字典,而我是C#的新手。

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#

推荐答案

一个数组是 string [] ,而不是列表< string> ,所以只要这样做:

An array is string[], not List<string>, so just do this:

Dictionary<string, string[]> wordDictionary = new Dictionary<string, string[]>();

现在,您可以照常添加阵列。

Now you can add your array as usual.

wordDictionary.Add("IN", IN);

或:

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"});

这篇关于在C#中添加一个数组到一个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆