Enumerable.OfType<TResult>(IEnumerable) Metoda

Definicja

Filtruje elementy elementu IEnumerable na podstawie określonego 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 do filtrowania elementów sekwencji.

Parametry

source
IEnumerable

Których elementy do filtrowania IEnumerable .

Zwraca

IEnumerable<TResult>

Element IEnumerable<T> zawierający elementy z sekwencji wejściowej typu TResult.

Wyjątki

source to null.

Przykłady

Poniższy przykład kodu przedstawia sposób filtrowania OfType elementów elementu 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

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony, wywołując metodę GetEnumerator bezpośrednio lub używając w foreach języku C# lub For Each w Visual Basic.

Metoda OfType<TResult>(IEnumerable) zwraca tylko te elementy, source które można rzutować, aby wpisać TResult. Aby zamiast tego otrzymać wyjątek, jeśli nie można rzutować elementu do wpisywania TResult, użyj polecenia Cast<TResult>(IEnumerable).

Ta metoda jest jedną z niewielu standardowych metod operatorów zapytań, które można zastosować do kolekcji, która ma typ niesparametryzowany, taki jak ArrayList. Dzieje się tak, ponieważ OfType rozszerza typ IEnumerable. OfType nie można stosować tylko do kolekcji opartych na typie sparametryzowanym, ale kolekcje oparte również na typie niesparametryzowanym IEnumerable<T>IEnumerable .

Stosując OfType do kolekcji, która implementuje IEnumerableusługę , uzyskujesz możliwość wykonywania zapytań względem kolekcji przy użyciu standardowych operatorów zapytań. Na przykład określenie argumentu typu , ObjectOfType który ma zwrócić obiekt typu IEnumerable<Object> w języku C# lub IEnumerable(Of Object) w Visual Basic, do którego można zastosować standardowe operatory zapytań.

Dotyczy