Enumerable.Cast<TResult>(IEnumerable) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 IEnumerable 的元素强制转换为指定的类型。
public:
generic <typename TResult>
[System::Runtime::CompilerServices::Extension]
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>
<Extension()>
Public Function Cast(Of TResult) (source As IEnumerable) As IEnumerable(Of TResult)
类型参数
- TResult
source
中的元素要强制转换成的类型。
参数
- source
- IEnumerable
包含要转换成类型 TResult
的元素的 IEnumerable。
返回
- IEnumerable<TResult>
一个 IEnumerable<T>,其中包含已强制转换为指定类型的源序列的每个元素。
例外
source
为 null
。
序列中的元素不能强制转换为 TResult
类型。
示例
下面的代码示例演示如何用于 Cast<TResult>(IEnumerable) 启用对某个 ArrayList标准查询运算符的使用。
System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("mango");
fruits.Add("apple");
fruits.Add("lemon");
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)
{
Console.WriteLine(fruit);
}
// This code produces the following output:
//
// apple
// lemon
// mango
' Create an ArrayList and add items to it.
Dim fruits As New System.Collections.ArrayList()
fruits.Add("mango")
fruits.Add("apple")
fruits.Add("lemon")
' 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
output.AppendLine(fruit)
Next
' Display the output.
Console.WriteLine(output.ToString())
' This code produces the following output:
'
' apple
' lemon
' mango
注解
此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 只有在通过直接调用其方法或在 Visual C# 中For Each
或Visual Basic中foreach
调用该GetEnumerator
对象,否则不会执行此方法表示的查询。
该方法 Cast<TResult>(IEnumerable) 允许通过提供必要的类型信息,在非泛型集合上调用标准查询运算符。 例如,不实现IEnumerable<T>,ArrayList但通过调用Cast<TResult>(IEnumerable)ArrayList对象,可以使用标准查询运算符来查询序列。
如果无法将元素转换为类型 TResult
,此方法将引发一个 InvalidCastException。
此方法的源序列是 IEnumerable,这意味着元素具有编译时静态类型 object
。 此方法执行的唯一类型转换是引用转换和取消装箱转换。 集合中元素的运行时类型必须与目标类型匹配,或者在值类型的情况下,元素的运行时类型必须是目标类型的装箱转换的结果。 不允许其他转换类型,例如不同数值类型之间的转换类型。
若要仅获取可转换为类型的 TResult
元素,请使用 OfType 方法而不是 Cast<TResult>(IEnumerable)。
在查询表达式中,显式类型的迭代变量转换为调用 Cast<TResult>(IEnumerable)。 此示例显示了显式类型化范围变量的语法。
from int i in objects
From i As Integer In objects
select
使用查询的子句执行其他转换类型,例如隐式数值转换。 以下示例使用 Cast
方法和 select
语句将装箱整数序列转换为双精度序列。
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)