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

IEnumerable篩選其項目。The IEnumerable whose elements to filter.

傳回

IEnumerable<TResult>

IEnumerable<T>其中包含型別的輸入序列中的項目TResultAn 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方法直接或透過foreachVisual C# 中或For EachVisual Basic 中。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.

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>型別,但根據非參數化的集合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. 例如,指定的類型引數ObjectOfType會傳回型別的物件IEnumerable<Object>在 C# 或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.

適用於