gpt4 book ai didi

c# - 在 C# 中将数组添加到字典

转载 作者:太空狗 更新时间:2023-10-29 20:53:13 25 4
gpt4 key购买 nike

我已尝试阅读有关此主题的其他帖子,但不太明白这一点。

我在 C# 中有一个列表,我想将其放入具有所有相同键的字典中。 list 是这个

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”(数组的名称),然后在字典中包含数组的每个字符串。

这是我为创建字典而写的(我不确定它是否正确):

Dictionary<string, List<string>> wordDictionary = new Dictionary<string, List<string>> ()

但我不确定如何填充字典。

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

最佳答案

一个数组是string[] , 不是 List<string> ,所以只需这样做:

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

现在您可以照常添加数组了。

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# - 在 C# 中将数组添加到字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com