Enumerable.OfType<TResult>(IEnumerable) 方法

定义

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

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

筛选序列元素所根据的类型。The type to filter the elements of the sequence on.

参数

source
IEnumerable

要对其元素进行筛选的 IEnumerableThe IEnumerable whose elements to filter.

返回

IEnumerable<TResult>

一个 包含类型为 TResult 的输入序列中的元素的 IEnumerable<T>An IEnumerable<T> that contains elements from the input sequence of type TResult.

异常

sourcenullsource is null.

示例

下面的代码示例演示如何使用 OfType 筛选 IEnumerable的元素。The following code example demonstrates how to use OfType to filter the elements of an 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

注解

此方法是使用延迟执行实现的。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 方法或在 Visual Basic 中使用视觉C#对象中的 foreachFor Each 来枚举对象。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.

OfType<TResult>(IEnumerable) 方法仅返回 source 中可强制转换为类型 TResult的元素。The OfType<TResult>(IEnumerable) method returns only those elements in source that can be cast to type TResult. 若要改为在元素无法强制转换为类型 TResult时接收异常,请使用 Cast<TResult>(IEnumerable)To instead receive an exception if an element cannot be cast to type TResult, use Cast<TResult>(IEnumerable).

此方法是一些标准查询运算符方法,可应用于具有非参数化类型的集合,如 ArrayListThis method is one of the few standard query operator methods that can be applied to a collection that has a non-parameterized type, such as an ArrayList. 这是因为 OfType 会将类型 IEnumerable扩展。This is because OfType extends the type IEnumerable. OfType 只能应用于基于参数化 IEnumerable<T> 类型的集合,而不能应用于基于非参数化 IEnumerable 类型的集合。OfType cannot only be applied to collections that are based on the parameterized IEnumerable<T> type, but collections that are based on the non-parameterized IEnumerable type also.

通过将 OfType 应用到实现 IEnumerable的集合,可以通过使用标准查询运算符来查询集合。By applying OfType to a collection that implements IEnumerable, you gain the ability to query the collection by using the standard query operators. 例如,将 Object 的类型参数指定为 OfType 将返回 Visual Basic 中 IEnumerable<Object> 类型的对象, C# IEnumerable(Of Object) 或者可以应用标准查询运算符。For example, specifying a type argument of Object to OfType would return an object of type IEnumerable<Object> in C# or IEnumerable(Of Object) in Visual Basic, to which the standard query operators can be applied.

适用于