gpt4 book ai didi

c# nested dictionary - 字典中的字典返回错误 (KeyNotFoundException)

转载 作者:行者123 更新时间:2023-11-30 22:07:13 24 4
gpt4 key购买 nike

当我尝试在 visual studio 2012 下的 asp.net web api 项目中使用 C# 中的嵌套字典时出现错误代码。此代码示例实际上是从其他支持者在 stack-overflow 上引用的。同样的方法适用于普通字典,但不适用于嵌套字典。

-谢谢

示例代码:

Dictionary<string, Dictionary<string, string>> nest_dict = new Dictionary<string, Dictionary<string, string>>();
nest_dict["a1"]["b1"] = "nest dict test string value";

An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll but was not handled in user code

最佳答案

您应该首先初始化“嵌套”字典:

Dictionary<string, Dictionary<string, string>> nest_dict = new Dictionary<string, Dictionary<string, string>>();
nest_dict["a1"] = new Dictionary<string, string>();
nest_dict["a1"]["b1"] = "nest dict test string value";

关于c# nested dictionary - 字典中的字典返回错误 (KeyNotFoundException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23149652/

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