I am aware of two methods of casting types to IEnumerable
from an Arraylist
in Linq and wondering in which cases to use them?
我知道在Linq中的Arraylist中有两种将类型转换为IEnumerable的方法,我想知道在哪些情况下应该使用它们?
e.g
如
IEnumerable<string> someCollection = arrayList.OfType<string>()
or
或
IEnumerable<string> someCollection = arrayList.Cast<string>()
What is the difference between these two methods and where should I apply each case?
这两种方法的区别是什么?我应该如何应用每种情况?
7 个解决方案
#1
247
OfType
- return only the elements of type x.Cast
- will try to cast all the elements into type x. if some of them are not from this type you will get InvalidCastException
OfType -只返回类型x的元素。Cast -将尝试将所有元素转换为类型x
EDIT
for example:
例如:编辑
object[] objs = new object[] { "12345", 12 };
objs.Cast<string>().ToArray(); //throws InvalidCastException
objs.OfType<string>().ToArray(); //return { "12345" }
#2
86
http://solutionizing.net/2009/01/18/linq-tip-enumerable-oftype/
http://solutionizing.net/2009/01/18/linq-tip-enumerable-oftype/
Fundamentally, Cast() is implemented like this:
基本上,Cast()是这样实现的:
public IEnumerable<T> Cast<T>(this IEnumerable source)
{
foreach(object o in source)
yield return (T) o;
}
Using an explicit cast performs well, but will result in an InvalidCastException if the cast fails. A less efficient yet useful variation on this idea is OfType():
使用显式强制转换执行得很好,但是如果强制转换失败,将导致InvalidCastException。在这个概念上,一个效率较低但有用的变体是OfType():
public IEnumerable<T> OfType<T>(this IEnumerable source)
{
foreach(object o in source)
if(o is T)
yield return (T) o;
}
The returned enumeration will only include elements that can safely be cast to the specified type.
返回的枚举只包含可以安全地转换为指定类型的元素。
#3
27
You should call Cast<string>()
if you know that all of the items are string
s.
If some of them aren't strings, you'll get an exception.
如果知道所有的项都是字符串,则应该调用Cast
You should call OfType<string>()
if you know that some of the items aren't string
s and you don't want those items.
If some of them aren't strings, they won't be in the new IEnumerable<string>
.
如果您知道某些项不是字符串,并且不想要这些项,那么应该调用OfType
#4
3
It should be noted that Cast(Of T) can be used on IEnumerable unlike other LINQ functions, so if there's ever a case where you need to use LINQ on a non-generic collection or list such as an ArrayList, you can use Cast(Of T) to cast to an IEnumerable(Of T) where LINQ can work.
应该注意的是,演员(T)可用于IEnumerable与其他LINQ功能,所以如果有一个情况需要使用LINQ非泛型集合或列表如一个ArrayList,您可以使用铸造(T)的演员IEnumerable接口(T),LINQ可以工作。
#5
2
Cast()
will try to cast all elements of the collection (and will throw an exception if element is of the wrong type) while OfType()
will return only elements of proper type.
Cast()将尝试转换集合的所有元素(如果元素为错误类型,将抛出异常),而OfType()将只返回正确类型的元素。
#6
2
OfType will filter the elements to return only the ones of the specified type. Cast will crash if an element cannot be cast to the target type.
OfType将筛选元素,只返回指定类型的元素。如果无法将元素强制转换为目标类型,则强制转换将崩溃。
#7
2
Cast will try to cast all items to the given type T. This cast could fail or throw an exception. OfType will return a subset of the original collection and return only objects that are of type T.
强制类型转换将尝试将所有项强制转换为给定类型t。此强制类型转换可能失败或抛出异常。OfType将返回原始集合的一个子集,只返回类型为T的对象。
#1
247
OfType
- return only the elements of type x.Cast
- will try to cast all the elements into type x. if some of them are not from this type you will get InvalidCastException
OfType -只返回类型x的元素。Cast -将尝试将所有元素转换为类型x
EDIT
for example:
例如:编辑
object[] objs = new object[] { "12345", 12 };
objs.Cast<string>().ToArray(); //throws InvalidCastException
objs.OfType<string>().ToArray(); //return { "12345" }
#2
86
http://solutionizing.net/2009/01/18/linq-tip-enumerable-oftype/
http://solutionizing.net/2009/01/18/linq-tip-enumerable-oftype/
Fundamentally, Cast() is implemented like this:
基本上,Cast()是这样实现的:
public IEnumerable<T> Cast<T>(this IEnumerable source)
{
foreach(object o in source)
yield return (T) o;
}
Using an explicit cast performs well, but will result in an InvalidCastException if the cast fails. A less efficient yet useful variation on this idea is OfType():
使用显式强制转换执行得很好,但是如果强制转换失败,将导致InvalidCastException。在这个概念上,一个效率较低但有用的变体是OfType():
public IEnumerable<T> OfType<T>(this IEnumerable source)
{
foreach(object o in source)
if(o is T)
yield return (T) o;
}
The returned enumeration will only include elements that can safely be cast to the specified type.
返回的枚举只包含可以安全地转换为指定类型的元素。
#3
27
You should call Cast<string>()
if you know that all of the items are string
s.
If some of them aren't strings, you'll get an exception.
如果知道所有的项都是字符串,则应该调用Cast
You should call OfType<string>()
if you know that some of the items aren't string
s and you don't want those items.
If some of them aren't strings, they won't be in the new IEnumerable<string>
.
如果您知道某些项不是字符串,并且不想要这些项,那么应该调用OfType
#4
3
It should be noted that Cast(Of T) can be used on IEnumerable unlike other LINQ functions, so if there's ever a case where you need to use LINQ on a non-generic collection or list such as an ArrayList, you can use Cast(Of T) to cast to an IEnumerable(Of T) where LINQ can work.
应该注意的是,演员(T)可用于IEnumerable与其他LINQ功能,所以如果有一个情况需要使用LINQ非泛型集合或列表如一个ArrayList,您可以使用铸造(T)的演员IEnumerable接口(T),LINQ可以工作。
#5
2
Cast()
will try to cast all elements of the collection (and will throw an exception if element is of the wrong type) while OfType()
will return only elements of proper type.
Cast()将尝试转换集合的所有元素(如果元素为错误类型,将抛出异常),而OfType()将只返回正确类型的元素。
#6
2
OfType will filter the elements to return only the ones of the specified type. Cast will crash if an element cannot be cast to the target type.
OfType将筛选元素,只返回指定类型的元素。如果无法将元素强制转换为目标类型,则强制转换将崩溃。
#7
2
Cast will try to cast all items to the given type T. This cast could fail or throw an exception. OfType will return a subset of the original collection and return only objects that are of type T.
强制类型转换将尝试将所有项强制转换为给定类型t。此强制类型转换可能失败或抛出异常。OfType将返回原始集合的一个子集,只返回类型为T的对象。