扩展方法ConvertAll
ConverAll 的正确用法是什么?它将一种类型转换为另一种类型吗?
像
List<int> intList = new List<int>();
intList.Add(10);
intList.Add(20);
intList.Add(30);
intList.Add(33);
var query= intList.ConvertAll(x=>(double)x);
这样我可以使用cast或OfType<>。
What is the proper use of ConverAll ? Will it convert one type to another type?
like
List<int> intList = new List<int>();
intList.Add(10);
intList.Add(20);
intList.Add(30);
intList.Add(33);
var query= intList.ConvertAll(x=>(double)x);
for this i can use cast or OfType<>.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ConvertAll
不是扩展方法,它是List
本身的一个真实方法。它返回一个包含转换后的元素的新列表。因此,在您的示例中,
query
变量实际上并不是一个查询,它是一个List
。Cast
和OfType
是扩展方法对IEnumerable
进行操作并返回IEnumerable
。但是它们不适合您声明的目的:Cast
可以转换引用类型,但不能转换值类型,只能将它们拆箱。OfType
不执行任何转换,它只是返回已经属于指定类型的任何元素。ConvertAll
isn't an extension method, it's a real method onList<T>
itself.It returns a new list containing the converted elements. So in your example, the
query
variable isn't actually a query, it's aList<double>
.Cast
andOfType
are extension methods that operate onIEnumerable
and return anIEnumerable<T>
. However they're not suitable for your stated purpose:Cast
can convert reference types but cannot convert value types, only unbox them.OfType
doesn't perform any conversion, it just returns any elements that are already of the specified type.ConvertAll
只会调用您的委托/列表中每个元素的匿名方法。这完全取决于你的作用。在您发布的示例代码中,它将尝试将每个元素转换为 double 并返回该元素,这意味着您将得到一个
List
作为回报。您不应使用
OfType
,因为这将根据类型过滤元素,并且仅当由于继承或接口实现而类型兼容时才会返回与原始类型不同的类型。换句话说,
.OfType
将不返回任何元素,因为没有一个整数也是双精度数。ConvertAll
will just call your delegate/anonymous method for each element of the list. What this does is entirely up to you.In the example code you posted, it will attempt to cast each element to a double and return that, which means you'll get a
List<Double>
in return.You should not use
OfType<T>
, since this will filter the elements based on the type, and will only return a different type than the original if it is type compatible due to inheritance or interface implementation.In other words,
.OfType<Double>
will return no elements, since none of the ints are also doubles.ConvertAll
是一个投影运算符,与 LINQ 的Select
映射最为接近。 LINQ 的Cast
是一个特定的投影运算符,代表做你所做的事情[通过投影] - 或者它会(如卢克的回答[和评论]中指出的,我+1)如果你是不转换为值类型。一般来说,LINQ 有一组更完整且经过深思熟虑的运算符,这使得像
ConvertAll
这样的旧东西有时看起来有点傻[像这样]。 (或 @stoopid :D)。ConvertAll
is a projection operator and maps most closely to LINQ'sSelect
. LINQ'sCast
is a specific projection operator and represents doing what you did [via projection] - or it would (as pointed out in Luke's answer [and comment], which I +1'd) if you weren't converting to a value type.In general, LINQ has a more complete and well-thought-through set of operators, which makes older stuff like
ConvertAll
look a bit silly at times [like this]. (or @stoopid :D).据我所知,
OfType
只会返回集合中指定类型 T 的元素。ConvertAll 允许您将元素转换为另一种类型。
to my knowledge,
OfType<T>
will only return the elements in the collection that are of the specified type T.ConvertAll allows you to convert the elements to another type.