Enumerable.OfType<TResult>(IEnumerable) メソッド

定義

指定された型に基づいて IEnumerable の要素をフィルター処理します。

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)

型パラメーター

TResult

シーケンスの要素をフィルター処理する型。

パラメーター

source
IEnumerable

フィルター処理する要素を含む IEnumerable

戻り値

IEnumerable<TResult>

TResult 型の入力シーケンスの要素を格納する IEnumerable<T>

例外

sourcenullです。

を使用 OfType して の要素をフィルター処理する方法を次のコード例に 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

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# または For Each Visual Basic で を使用foreachして列挙されるまで実行されません。

メソッドは OfType<TResult>(IEnumerable) 、 型にキャストできる 内の source 要素のみを返します TResult。 要素を 型 TResultにキャストできない場合に例外を受け取る場合は、 を使用します Cast<TResult>(IEnumerable)

このメソッドは、 などのパラメーター化されていない型を持つコレクションに適用できる数少ない標準クエリ演算子メソッドの ArrayList1 つです。 これは、 型IEnumerableを拡張するためOfTypeです。 OfTypeは、パラメーター化された型に基づくコレクションにのみ適用できるわけではありませんが、パラメーター化IEnumerable<T>IEnumerableされていない型に基づくコレクションにも適用できます。

OfType 実装 IEnumerableするコレクションにを適用すると、標準のクエリ演算子を使用してコレクションに対してクエリを実行できます。 たとえば、 の型引数Objectを にOfType指定すると、C# または IEnumerable(Of Object) Visual Basic で型IEnumerable<Object>のオブジェクトが返され、標準クエリ演算子を適用できます。

適用対象