Enumerable.OfType<TResult>(IEnumerable) 方法

定义

根据指定类型筛选 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>

一个 IEnumerable<T> 包含类型 TResult输入序列中的元素。

例外

sourcenull

示例

下面的代码示例演示如何用于 OfType 筛选元素 IEnumerable

System.Collections.ArrayList fruits = new()
{
    "Mango",
    "Orange",
    null,
    "Apple",
    3.0,
    "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.Contains('n', StringComparison.CurrentCultureIgnoreCase));

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 ArrayList() From {
    "Mango",
    "Orange",
    Nothing,
    "Apple",
    3.0,
    "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.Contains("n"c, StringComparison.CurrentCultureIgnoreCase))

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

注解

此方法是使用延迟执行实现的。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 除非通过直接调用其 GetEnumerator 方法或在 C# 中使用 foreach 或在 Visual Basic 中使用 For Each 来枚举对象,否则不会执行此方法表示的查询。

该方法OfType<TResult>(IEnumerable)仅返回那些非 null 且与类型TResult兼容的元素source。 若要在无法将元素强制转换为类型 TResult时接收异常,请使用 Cast<TResult>(IEnumerable)

此方法是少数标准查询运算符方法之一,可应用于具有非参数化类型的集合,例如 ArrayList。 这是因为 OfType 扩展了类型 IEnumerableOfType 不能仅应用于基于参数化 IEnumerable<T> 类型的集合,而基于非参数化 IEnumerable 类型的集合也适用。

通过应用于 OfType 实现的 IEnumerable集合,可以使用标准查询运算符来查询集合。 例如,将 Object 的类型参数指定为 OfType 将返回 C# 中类型为 IEnumerable<Object> 的对象,或者在Visual Basic中返回IEnumerable(Of Object)的对象,标准查询运算符可以应用于该对象。

适用于