Skip to main content
 首页 » 编程设计

c#之为什么字典 "does not contain a definition for ' ElementAt'”

2024年02月05日10mengfanrong

为什么字典在动态规划期间“不包含‘ElementAt’的定义”

        Dictionary<string, dynamic> D1 = new Dictionary<string, dynamic>(); 
        D1.Add("w1", 10); 
        D1.Add("w2", false); 
        Dictionary<string, dynamic> D2 = new Dictionary<string, dynamic>(); 
        D2.Add("v1", 10); 
        D2.Add("v2", D1); 
        textBox1.Text += D2.ElementAt(1).Value.ElementAt(1).Value; 

我们应该在 textbox1 上得到结果“false”
但是我们会得到运行时错误:“不包含‘ElementAt’的定义”

如果你要输入:

        Dictionary<string, dynamic> var1 = D2.ElementAt(1).Value; 
        textBox1.Text += var1.ElementAt(1).Value; 

然后就可以正常工作了!

请您参考如下方法:

另一个解决方案,对我来说,我需要添加对 System.Linq 的引用并且一切正常,无需修改所需的代码。