为什么不能完成投射? [英] Why can't this cast be done?

查看:90
本文介绍了为什么不能完成投射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有编程问题,但我想知道为什么.

我有类似的东西

I don''t have a programming problem, but I''d like to know why.

I have something like this

List<MyClass> a,b,c<br /><br />var d = a.Except(b);<br />c.Clear()<br />c = d.ToList();



当然,列出& b在代码之前填充.问题是,为什么我不能直接投射到List< myclass>这样的:



Of course, lists a & b are populated preceding the code. The question is, why can''t I cast directly to List<myclass> as such:

<br />c = a.Except(b);<br />





推荐答案

您不能将其强制转换为a.Except(b )返回类型System.Linq.Enumerable.ExceptIterator.您在此处有两个选择:
You can''t cast it as such because a.Except(b) returns the type System.Linq.Enumerable.ExceptIterator. You have two options here:
c.AddRange(a.Except(b));

c = a.Except(b).ToList();






,因为它们基本上是不同的类型. a.Except(b)不返回列表,而是返回"Enumerable"类型.

所以

because they''re basically different types. a.Except(b) does not return a list, it returns an "Enumerable" type.

so

IEnumerable c
List a,b

c = a.Except(b)


会起作用


will work


这篇关于为什么不能完成投射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆