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 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

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉物件,否則不會執行這個方法所代表的查詢。

方法 OfType<TResult>(IEnumerable) 只會傳回中 source 可以轉換成 類型的 TResult 專案。 若要改為在專案無法轉換成類型 TResult 時收到例外狀況,請使用 Cast<TResult>(IEnumerable)

這個方法是幾個標準查詢運算子方法之一,可以套用至具有非參數化型別的集合,例如 ArrayList 。 這是因為 OfType 會擴充 型別 IEnumerableOfType 不能只套用至以參數化 IEnumerable<T> 型別為基礎的集合,但以非參數化 IEnumerable 型別為基礎的集合也一併套用。

藉由套用 OfType 至實作 的 IEnumerable 集合,您可以使用標準查詢運算子來查詢集合。 例如,指定 的型別引數 ObjectOfType 傳回 C# 或 IEnumerable(Of Object) Visual Basic 中型 IEnumerable<Object> 別的物件,以便套用標準查詢運算子。

適用於