【发布时间】:2016-09-30 17:18:36
【问题描述】:
IDictionary<TKey, TValue> 扩展自接口ICollection<KeyValuePair<TKey, TValue>>,所以它有一个Add(KeyValuePair<TKey, TValue> item) 方法:
IDictionary<string, object> dic = new Dictionary<string, object>();
dic.Add(new KeyValuePair<string, object>("number", 42)); // Compiles fine
不过,Dictionary<TKey, Tvalue>虽然实现了IDictionary<TKey, TValue>,但没有这个方法重载:
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add(new KeyValuePair<string, object>("number", 42)); // Does not compile
这怎么可能?
【问题讨论】:
标签: c# .net dictionary interface