Enumerable.OfType<TResult>(IEnumerable) Metoda

Definice

Filtruje prvky 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 pro filtrování prvků sekvence na.

Parametry

source
IEnumerable

Jejíž IEnumerable prvky chcete filtrovat.

Návraty

IEnumerable<TResult>

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

Výjimky

source je null.

Příklady

Následující příklad kódu ukazuje, jak použít OfType k filtrování prvků objektu IEnumerable.

System.Collections.ArrayList fruits = new()
{
    "Mango",
    "Orange",
    null,
    "Apple",
    3.0,
    "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.Contains('n', StringComparison.CurrentCultureIgnoreCase));

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 ArrayList() From {
    "Mango",
    "Orange",
    Nothing,
    "Apple",
    3.0,
    "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.Contains("n"c, StringComparison.CurrentCultureIgnoreCase))

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 se nespustí, dokud objekt nevyčte buď voláním metody GetEnumerator přímo nebo pomocí foreach v jazyce C# nebo For Each v Visual Basic.

Metoda OfType<TResult>(IEnumerable) vrátí pouze ty prvky, source které nejsou null a kompatibilní s typem TResult. Chcete-li přijmout výjimku, pokud prvek nelze přetypovat na typ TResult, použijte Cast<TResult>(IEnumerable).

Tato metoda je jednou z několika metod standardního operátoru dotazu, které lze použít v kolekci, která má neparametrizovaný typ, například ArrayList. Je to proto, že OfType rozšiřuje typ IEnumerable. OfType nelze použít pouze u kolekcí, které jsou založeny na parametrizovaném IEnumerable<T> typu, ale kolekce založené také na neparametrizovaném IEnumerable typu.

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

Platí pro