【问题标题】:Error: Can't convert from Dictionary to IDictionary错误:无法从 Dictionary 转换为 IDictionary
【发布时间】:2026-01-19 01:45:01
【问题描述】:

为什么会出现错误 错误 52 参数 1:无法从 'System.Collections.Generic.Dictionary>' 转换为 'System.Collections.Generic.IDictionary>'

Dictionary<string, List<string>> tempResultIDList = new Dictionary<string,List<string>>();
test(tempResultIDList);

public bool test(IDictionary<string,IList<string>> a)
  {
            return true;
    }

【问题讨论】:

标签: dictionary idictionary


【解决方案1】:

Dictionary&lt;string, List&lt;string&gt;&gt; 实现 IDictionary&lt;string, List&lt;string&gt;&gt;,而您正尝试将其转换为 IDictionary&lt;string, *I*List&lt;string&gt;&gt;。这是不允许的,因为IDictionary&lt;string, IList&lt;string&gt;&gt; 有例如方法Add 接受IList&lt;string&gt; 的实例,而Dictionary&lt;string, List&lt;string&gt;&gt; 中的Add 方法不接受IList&lt;string&gt; 作为其输入。

【讨论】:

    最近更新 更多