Error: Can't convert from Dictionary to IDicti

2020-03-30 01:00发布

问题:

Why is there an error Error 52 Argument 1: cannot convert from 'System.Collections.Generic.Dictionary>' to '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;
    }

回答1:

Dictionary<string, List<string>> implements IDictionary<string, List<string>>, while you're trying to cast it to IDictionary<string, *I*List<string>>. It is not allowed, because IDictionary<string, IList<string>> has e.g. the method Add accepting instance of IList<string>, while Add method in your Dictionary<string, List<string>> won't accept IList<string> as its input.