为什么不能将 List<decimal> 转换为 List<int>?

Why cant List<decimal> be casted to List<int>?

下面的代码给出了一个InvalidCast异常

int newValue = new List<decimal>() { 6m }.Cast<int>().FirstOrDefault();

虽然decimal可以投int,为什么不能在列表中投呢?

编辑: 为了解决这个问题,我想知道为什么等式的 cast 部分会抛出异常。只是 运行 new List<decimal>() { 6m }.Cast<int>().ToList() 也会给出 InvalidCast 异常

你需要:

new List<decimal>() { 6m}.Select(d => (int)d).ToList<int>();

new List<decimal>() { 6m}.ConvertAll(d => (int)d);

Select 与任何 IEnumerable 一起使用,ConvertAll 仅适用于 List

.Cast 应该在需要处理(例如)数组列表的成员时使用,就好像它们是强类型的。

感谢@hvd 的指正