Enumerable.OfType<TResult>(IEnumerable) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Filtra gli elementi di un IEnumerable in base a un tipo specificato.
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)
Parametri di tipo
- TResult
Tipo in cui filtrare gli elementi della sequenza.
Parametri
- source
- IEnumerable
Oggetto di IEnumerable cui filtrare gli elementi.
Valori restituiti
Oggetto IEnumerable<T> che contiene elementi della sequenza di input di tipo TResult.
Eccezioni
source è null.
Esempio
Nell'esempio di codice seguente viene illustrato come usare OfType per filtrare gli elementi di un oggetto 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
Commenti
Questo metodo viene implementato usando l'esecuzione posticipata. Il valore restituito immediato è un oggetto che archivia tutte le informazioni necessarie per eseguire l'azione. La query rappresentata da questo metodo non viene eseguita finché l'oggetto non viene enumerato chiamando direttamente il relativo metodo GetEnumerator o usando foreach in C# o For Each in Visual Basic.
Il OfType<TResult>(IEnumerable) metodo restituisce solo gli elementi in source che sono non Null e compatibili con il tipo TResult. Per ricevere un'eccezione se non è possibile eseguire il cast di un elemento al tipo TResult, usare Cast<TResult>(IEnumerable).
Questo metodo è uno dei pochi metodi dell'operatore di query standard che possono essere applicati a una raccolta con un tipo non con parametri, ad esempio .ArrayList Ciò è dovuto al fatto che OfType estende il tipo IEnumerable. OfType non può essere applicato solo alle raccolte basate sul tipo con IEnumerable<T> parametri, ma anche raccolte basate sul tipo non con parametri IEnumerable .
OfType Applicando a una raccolta che implementa IEnumerable, si ottiene la possibilità di eseguire query sulla raccolta usando gli operatori di query standard. Ad esempio, specificando un argomento di tipo di Object a OfType restituirebbe un oggetto di tipo IEnumerable<Object> in C# o IEnumerable(Of Object) in Visual Basic, a cui è possibile applicare gli operatori di query standard.