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 篩選的元素 IEnumerableThe 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 foreach Visual c # 或 Visual Basic 中的方法來列舉物件為止 For EachThe 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 可以轉換成類型的元素 TResultThe 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 擴充型別 IEnumerableThis is because OfType extends the type IEnumerable. OfType 只能套用至以參數化型別為基礎的集合,但也不能套用至以 IEnumerable<T> 非參數化型別為基礎的集合 IEnumerableOfType 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.

適用於