gpt4 book ai didi

c# - 从列表访问字典时出现异常

转载 作者:行者123 更新时间:2023-11-30 19:42:01 26 4
gpt4 key购买 nike

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
class Program
{
static void Main(string[] args)
{
List<object> list = new List<object>();
List<Dictionary<string, object>> dict = new List<Dictionary<string, object>>();

Dictionary<string, object> master = new Dictionary<string, object>();
master.Add("list", list);
master.Add("dict", dict);

List<object> mydict = (List<object>)master["dict"]; // this is where i get exception
Console.Write("Count: ", mydict.Count);
}
}
}

它在粗线处抛出异常。为什么会出现这种行为,我该如何访问这个元素?谢谢苏曼特

最佳答案

因为没有List<object>在字典键下

var mydict = (List<Dictionary<string, object>>)master["dict"];

关于c# - 从列表访问字典时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18205043/

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