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>입니다.

예외

source이(가) null인 경우

예제

다음 코드 예제에서는 를 사용하여 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) 형식TResult으로 캐스팅할 수 있는 의 source 요소만 반환합니다. 요소를 형식 TResult으로 캐스팅할 수 없는 경우 대신 예외를 받으려면 를 사용합니다 Cast<TResult>(IEnumerable).

이 메서드는 와 같이 매개 변수가 없는 형식의 컬렉션에 적용할 수 있는 몇 안 되는 표준 쿼리 연산자 ArrayList메서드 중 하나입니다. 형식을 확장하기 IEnumerable때문 OfType 입니다. OfType 매개 변수가 IEnumerable<T> 있는 형식을 기반으로 하는 컬렉션에만 적용할 수 없지만 매개 변수 IEnumerable 가 없는 형식을 기반으로 하는 컬렉션도 적용할 수 있습니다.

를 구현IEnumerable하는 OfType 컬렉션에 적용하면 표준 쿼리 연산자를 사용하여 컬렉션을 쿼리할 수 있습니다. 예를 들어 의 형식 인수 Object 를 지정하면 표준 쿼리 연산자를 적용할 OfType 수 있는 형식의 IEnumerable<Object> 개체가 C# 또는 IEnumerable(Of Object) Visual Basic에서 반환됩니다.

적용 대상