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>

IEnumerable<T> 형식TResult의 입력 시퀀스의 요소를 포함하는 요소입니다.

예외

sourcenull입니다.

예제

다음 코드 예제에서는 요소를 필터링 하는 데 사용 OfType 하는 방법을 보여 줍니다 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

설명

이 메서드는 지연된 실행을 사용하여 구현됩니다. 즉시 반환 값은 작업을 수행하는 데 필요한 모든 정보를 저장하는 개체입니다. 이 메서드가 나타내는 쿼리는 해당 GetEnumerator 메서드를 직접 호출하거나 C#의 foreach 또는 Visual Basic For Each 사용하여 개체가 열거될 때까지 실행되지 않습니다.

메서드는 OfType<TResult>(IEnumerable) null이 아니고 형식source과 호환되는 요소 TResult 만 반환합니다. 요소를 형식 TResult으로 캐스팅할 수 없는 경우 예외를 받으려면 .를 사용합니다 Cast<TResult>(IEnumerable).

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

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

적용 대상