Tag: ienumerable hashset

C#类型转换:显式转换存在但引发转换错误?

我了解到HashSet实现了IEnumerable接口。 因此,可以将HashSet对象隐式转换为IEnumerable : HashSet foo = new HashSet(); IEnumerable foo2 = foo; // Implicit cast, everything fine. 这也适用于嵌套generics类型: HashSet<HashSet> dong = new HashSet<HashSet>(); IEnumerable<IEnumerable> dong2 = dong; // Implicit cast, everything fine. 至少这就是我的想法。 但如果我创建一个Dictionary ,我会遇到一个问题: IDictionary<T, HashSet> bar = new Dictionary<T, HashSet>(); IDictionary<T, IEnumerable> bar2 = bar; // compile error 最后一行给出了以下编译错误(Visual Studio 2015): 无法隐式转换类型 System.Collections.Generic.IDictionary<T, […]