Enumerable.Cast<TResult>(IEnumerable) 方法


IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

generic <typename TResult>
 static System::Collections::Generic::IEnumerable<TResult> ^ Cast(System::Collections::IEnumerable ^ source);
public static System.Collections.Generic.IEnumerable<TResult> Cast<TResult> (this System.Collections.IEnumerable source);
static member Cast : System.Collections.IEnumerable -> seq<'Result>
Public Function Cast(Of TResult) (source As IEnumerable) As IEnumerable(Of TResult)



source 中的元素要强制转换成的类型。The type to cast the elements of source to.



包含要转换成类型 TResult 的元素的 IEnumerableThe IEnumerable that contains the elements to be cast to type TResult.



一个 IEnumerable<T>,其中包含已强制转换为指定类型的源序列的每个元素。An IEnumerable<T> that contains each element of the source sequence cast to the specified type.


sourcenullsource is null.

序列中的元素不能强制转换为 TResult 类型。An element in the sequence cannot be cast to type TResult.


下面的代码示例演示如何使用 Cast<TResult>(IEnumerable) 在上启用标准查询运算符 ArrayListThe following code example demonstrates how to use Cast<TResult>(IEnumerable) to enable the use of the standard query operators on an ArrayList.

System.Collections.ArrayList fruits = new System.Collections.ArrayList();

IEnumerable<string> query =
    fruits.Cast<string>().OrderBy(fruit => fruit).Select(fruit => fruit);

// The following code, without the cast, doesn't compile.
//IEnumerable<string> query1 =
//    fruits.OrderBy(fruit => fruit).Select(fruit => fruit);

foreach (string fruit in query)

// This code produces the following output:
// apple
// lemon
// mango
' Create an ArrayList and add items to it.
Dim fruits As New System.Collections.ArrayList()

' Call Cast(Of String) to cast the ArrayList elements to strings.
Dim query As IEnumerable(Of String) =
fruits.Cast(Of String)().OrderBy(Function(fruit) fruit).Select(Function(fruit) fruit)

'' The following code, without the cast, doesn't compile.
'Dim query As IEnumerable(Of String) = _
'    fruits.OrderBy(Function(fruit) fruit).Select(Function(fruit) fruit)

Dim output As New System.Text.StringBuilder
For Each fruit As String In query

' Display the output.

' This code produces the following output:
' apple
' lemon
' mango


此方法是使用延迟执行实现的。This method is implemented by using deferred execution. 即时返回值是一个对象,该对象存储执行操作所需的所有信息。The immediate return value is an object that stores all the information that is required to perform the action. 此方法表示的查询在枚举对象之前不会执行,方法是直接调用其 GetEnumerator 方法,或者通过 foreach 在 Visual c # 中使用或 For Each 在 Visual Basic 中使用。The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

Cast<TResult>(IEnumerable)方法通过提供必要的类型信息,对非泛型集合调用标准查询运算符。The Cast<TResult>(IEnumerable) method enables the standard query operators to be invoked on non-generic collections by supplying the necessary type information. 例如,不 ArrayList 实现 IEnumerable<T> ,但通过 Cast<TResult>(IEnumerable)ArrayList 对象调用,然后可以使用标准查询运算符来查询序列。For example, ArrayList does not implement IEnumerable<T>, but by calling Cast<TResult>(IEnumerable) on the ArrayList object, the standard query operators can then be used to query the sequence.

如果元素无法转换为类型,则 TResult 此方法将引发 InvalidCastExceptionIf an element cannot be converted to type TResult, this method throws a InvalidCastException.

此方法的源序列为 IEnumerable ,这意味着元素具有的编译时静态类型 objectThe source sequence for this method is IEnumerable, which means the elements have the compile-time static type of object. 此方法执行的唯一类型转换是引用转换和取消装箱转换。The only type conversions that are performed by this method are reference conversions and unboxing conversions. 集合中元素的运行时类型必须与目标类型匹配,或者,对于值类型,元素的运行时类型必须是目标类型的装箱转换的结果。The runtime type of the elements in the collection must match the target type, or in the case of value types, the runtime type of elements must be the result of a boxing conversion of the target type. 不允许使用其他转换类型,如不同数值类型之间的转换类型。Other conversion types, such as those between different numeric types, are not allowed.

若要仅获取可以转换为类型的元素 TResult ,请使用 OfType 方法而不是 Cast<TResult>(IEnumerable)To obtain only those elements that can be converted to type TResult, use the OfType method instead of Cast<TResult>(IEnumerable).

在查询表达式中,显式类型迭代变量转换为对的调用 Cast<TResult>(IEnumerable)In a query expression, an explicitly typed iteration variable translates to an invocation of Cast<TResult>(IEnumerable). 此示例演示显式类型化范围变量的语法。This example shows the syntax for an explicitly typed range variable.

from int i in objects  
From i As Integer In objects  

使用 select 查询子句来执行其他转换类型,如隐式数值转换。Use the select clause of a query to perform other conversion types, like the implicit numeric conversions. 下面的示例使用 Cast 方法和 select 语句将一系列装箱的整数转换为双精度序列。The following example uses both the Cast method and a select statement to convert a sequence of boxed integers to a sequence of doubles.

IEnumerable sequence = Enumerable.Range(0, 10);
var doubles = from int item in sequence
                select (double)item;
Dim sequence As IEnumerable = Enumerable.Range(0, 10)
Dim doubles = From item As Integer In sequence
                Select CType(item, Double)