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, System.Collections.Generic.HashSet>
toSystem.Collections.Generic.IDictionary<T, System.Collections.Generic.IEnumerable>
。存在显式转换(您是否错过了演员?)
但如果我通过写作进行演员表演
IDictionary<T, IEnumerable> bar2 = (IDictionary<T, IEnumerable>) bar;
然后我在运行时获得了无效的强制转换exception。
两个问题:
它不起作用的原因是IDictionary
值不是共变体 (并且也是关键,出于同样的原因)。 如果允许,则此代码将编译,但必须导致exception:
IDictionary> foo = new Dictionary>(); IDictionary> bar = foo; foo.Add(key, new List ());
您认为添加List
会起作用,因为它会编译,因为值类型应该是IEnumerable
。 但是,它不能成功,因为实际值类型是HashSet
。
所以,是的:唯一的方法是创建一个新的字典。
var bar = foo.ToDictionary(x => x.Key, x => x.Value.AsEnumerable());
我该如何解决这个问题? 是迭代密钥并逐步构建新字典的唯一方法吗?
它可能不是最优雅的解决方案,但它有效:
IDictionary> bar = new Dictionary>(); IDictionary> bar2 = bar.ToDictionary(x => x.Key, y => (IEnumerable )y.Value);
Dictionary强制转换不起作用的原因是因为IEnumerable
是共变量,请注意声明中的
public interface IEnumerable : IEnumerable
IDictionary
不是。
public interface IDictionary : ICollection>, IEnumerable>, IEnumerable
你可以在这里阅读更多相关信息: https : //msdn.microsoft.com/en-us/library/dd799517(v = vs1010).aspx
上述就是C#学习教程:C#类型转换:显式转换存在但引发转换错误?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1017012.html