Enumerable.OfType<TResult>(IEnumerable) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
根据指定类型筛选 IEnumerable 的元素。
public:
generic <typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ OfType(System::Collections::IEnumerable ^ source);
public static System.Collections.Generic.IEnumerable<TResult> OfType<TResult> (this System.Collections.IEnumerable source);
static member OfType : System.Collections.IEnumerable -> seq<'Result>
<Extension()>
Public Function OfType(Of TResult) (source As IEnumerable) As IEnumerable(Of TResult)
类型参数
- TResult
筛选序列元素所根据的类型。
参数
- source
- IEnumerable
要对其元素进行筛选的 IEnumerable。
返回
- IEnumerable<TResult>
一个 包含类型为 TResult
的输入序列中的元素的 IEnumerable<T>。
例外
source
为 null
。
示例
下面的代码示例演示如何用于 OfType 筛选元素 IEnumerable。
System.Collections.ArrayList fruits = new System.Collections.ArrayList(4);
fruits.Add("Mango");
fruits.Add("Orange");
fruits.Add("Apple");
fruits.Add(3.0);
fruits.Add("Banana");
// Apply OfType() to the ArrayList.
IEnumerable<string> query1 = fruits.OfType<string>();
Console.WriteLine("Elements of type 'string' are:");
foreach (string fruit in query1)
{
Console.WriteLine(fruit);
}
// The following query shows that the standard query operators such as
// Where() can be applied to the ArrayList type after calling OfType().
IEnumerable<string> query2 =
fruits.OfType<string>().Where(fruit => fruit.ToLower().Contains("n"));
Console.WriteLine("\nThe following strings contain 'n':");
foreach (string fruit in query2)
{
Console.WriteLine(fruit);
}
// This code produces the following output:
//
// Elements of type 'string' are:
// Mango
// Orange
// Apple
// Banana
//
// The following strings contain 'n':
// Mango
// Orange
// Banana
' Create an ArrayList and add items to it.
Dim fruits As New System.Collections.ArrayList(4)
fruits.Add("Mango")
fruits.Add("Orange")
fruits.Add("Apple")
fruits.Add(3.0)
fruits.Add("Banana")
' Apply OfType(Of String)() to the ArrayList
' to filter out non-string items.
Dim query1 As IEnumerable(Of String) = fruits.OfType(Of String)()
' Print the results.
Dim output As New System.Text.StringBuilder("Elements of type 'string' are:" _
& vbCrLf)
For Each fruit As String In query1
output.AppendLine(fruit)
Next
' The following query shows that the standard query operators such as
' Where() can be applied to the ArrayList type after calling OfType().
Dim query2 As IEnumerable(Of String) =
fruits.OfType(Of String)().Where(Function(fruit) _
fruit.ToLower().Contains("n"))
output.AppendLine(vbCrLf & "The following strings contain 'n':")
For Each fruit As String In query2
output.AppendLine(fruit)
Next
' Display the output.
Console.WriteLine(output.ToString())
' This code produces the following output:
'
' Elements of type 'string' are:
' Mango
' Orange
' Apple
' Banana
'
' The following strings contain 'n':
' Mango
' Orange
' Banana
注解
此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 只有在通过直接调用其方法或在 Visual C# 中For Each
或Visual Basic中foreach
调用该GetEnumerator
对象,否则不会执行此方法表示的查询。
该方法OfType<TResult>(IEnumerable)仅返回可以强制转换为类型的TResult
元素source
。 若要改为接收异常(如果元素无法强制转换为类型 TResult
,请使用 Cast<TResult>(IEnumerable))。
此方法是可应用于具有非参数化类型的集合的少数标准查询运算符方法之一 ArrayList,例如。 这是因为 OfType 扩展类型 IEnumerable。 OfType 不能仅应用于基于参数化 IEnumerable<T> 类型的集合,而基于非参数化 IEnumerable 类型的集合也适用。
通过应用于 OfType 实现的 IEnumerable集合,可以使用标准查询运算符来查询集合。 例如,指定要在 C# 或 IEnumerable(Of Object)
Visual Basic 中返回类型IEnumerable<Object>
对象的类型参数ObjectOfType,可以向其应用标准查询运算符。