Enumerable.OfType<TResult>(IEnumerable) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Filtruje prvky IEnumerable založené na zadaném 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ý chcete filtrovat prvky sekvence.
Parametry
- source
- IEnumerable
Jejíž IEnumerable prvky se mají filtrovat.
Návraty
- IEnumerable<TResult>
Prvek IEnumerable<T> , který obsahuje 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 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 se nespustí, dokud objekt nevyčítá buď voláním metody GetEnumerator
přímo, nebo pomocí foreach
v jazyce Visual C# nebo For Each
v Visual Basic.
Metoda OfType<TResult>(IEnumerable) vrátí pouze ty prvky, které source
lze přetypovat na typ TResult
. Chcete-li 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 několika metod standardního operátoru dotazu, které lze použít u kolekce, která má neparametrizovaný typ, například ArrayList. To je proto, že OfType rozšiřuje typ IEnumerable. OfType nelze použít pouze u kolekcí, které jsou založeny na parametrizovaném typu, ale kolekce, které jsou založeny na neparametrizovaném IEnumerable<T> IEnumerable typu.
Když použijete OfType kolekci, která implementuje IEnumerable, získáte možnost dotazovat se na kolekci pomocí standardních operátorů dotazů. Například zadáním argumentu Object typu, který má být OfType vrácen objekt typu IEnumerable<Object>
v jazyce C# nebo IEnumerable(Of Object)
v Visual Basic, na který lze použít standardní operátory dotazů.