Enumerable.OfType(IEnumerable) Enumerable.OfType(IEnumerable) Enumerable.OfType(IEnumerable) Enumerable.OfType(IEnumerable) Method

定義

根據指定的型別來篩選 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 IEnumerable IEnumerable 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.

例外狀況

範例

下列程式碼範例示範如何使用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.
MsgBox(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 C#或For Each Visual Basic 中使用foreach來列舉物件之後, 才會執行。The 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.

方法只會傳回中source可以轉換成類型TResult的元素。 OfType<TResult>(IEnumerable)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.

藉由將套用IEnumerable至執行的集合,您可以使用標準查詢運算子來查詢集合。OfTypeBy applying OfType to a collection that implements IEnumerable, you gain the ability to query the collection by using the standard query operators. 例如, Object指定的OfType型別引數會傳回C#或中類型IEnumerable<Object>的物件, 也IEnumerable(Of Object)就是可套用標準查詢運算子的 Visual Basic。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.

適用於