Enumerable.OfType<TResult>(IEnumerable) Metoda

Definice

Filtruje prvky objektu IEnumerable na základě zadaného typu.

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)

Parametry typu

TResult

Typ, na který se mají filtrovat prvky sekvence.

Parametry

source
IEnumerable

Objekt IEnumerable , jehož prvky se mají filtrovat.

Návraty

IEnumerable<TResult>

Obsahuje IEnumerable<T> elementy ze vstupní sekvence typu TResult.

Výjimky

source je null.

Příklady

Následující příklad kódu ukazuje použití OfType k filtrování prvků objektu 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

Poznámky

Tato metoda se implementuje pomocí odloženého spuštění. Okamžitá návratová hodnota je objekt, který ukládá všechny informace potřebné k provedení akce. Dotaz reprezentovaný touto metodou není proveden, dokud objekt není výčet buď voláním jeho GetEnumerator metody přímo, nebo pomocí foreach v jazyce C# nebo For Each v jazyce Visual Basic.

Metoda OfType<TResult>(IEnumerable) vrátí pouze ty prvky, source které lze přetypovat na typ TResult. Pokud chcete místo toho obdržet výjimku, pokud prvek nelze přetypovat na typ TResult, použijte Cast<TResult>(IEnumerable).

Tato metoda je jednou z mála standardních metod operátoru dotazu, kterou je možné použít u kolekce s neparametrizovaným typem ArrayList, jako je například . Je to proto, že OfType rozšiřuje typ IEnumerable. OfType nelze použít pouze pro kolekce, které jsou založeny na parametrizovaném IEnumerable<T> typu, ale kolekce, které jsou založeny na neparametrizovaném IEnumerable typu.

Použitím na OfType kolekci, která implementuje IEnumerable, získáte možnost dotazovat se na kolekci pomocí standardních operátorů dotazu. Například zadání argumentu Object typu do OfType vrátí objekt typu IEnumerable<Object> v jazyce C# nebo IEnumerable(Of Object) Visual Basic, na který lze použít standardní operátory dotazu.

Platí pro