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)方法僅回傳非空且與型別source相容的元素TResult。 若元素無法轉換為型別 TResult,則要接收例外,請使用 Cast<TResult>(IEnumerable)

此方法是少數可應用於非參數化型別(如 ArrayList。 這是因為 OfType 擴展了型別 IEnumerableOfType 不僅適用於基於參數化 IEnumerable<T> 型別的集合,也適用於基於非參數化 IEnumerable 型別的集合。

透過應用 OfType 於實作 的 IEnumerable集合,你可以利用標準查詢運算子查詢該集合。 例如,將型別參數 Object 指定為 OfType,則會在 C# 中回傳型為 IEnumerable<Object>,或在 Visual Basic 中回傳為 IEnumerable(Of Object),標準查詢運算子可用於此。

適用於