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>

IEnumerable<T>,其中包含輸入序列中型別為 TResult 的項目。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#使用 Visual 或 For Each 中的 foreachThe 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 會傳回類型為 IEnumerable<Object> 的物件C# ,或 Visual Basic 中可套用標準查詢運算子的 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.

適用於