Enumerable.Cast<TResult>(IEnumerable) 메서드

정의

IEnumerable의 요소를 지정된 형식으로 캐스팅합니다.

public:
generic <typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ Cast(System::Collections::IEnumerable ^ source);
public static System.Collections.Generic.IEnumerable<TResult> Cast<TResult> (this System.Collections.IEnumerable source);
static member Cast : System.Collections.IEnumerable -> seq<'Result>
<Extension()>
Public Function Cast(Of TResult) (source As IEnumerable) As IEnumerable(Of TResult)

형식 매개 변수

TResult

source의 요소를 캐스팅할 형식입니다.

매개 변수

source
IEnumerable

TResult 형식으로 캐스팅할 요소가 들어 있는 IEnumerable입니다.

반환

IEnumerable<TResult>

지정된 형식으로 캐스트된 소스 시퀀스의 각 요소가 들어 있는 IEnumerable<T>입니다.

예외

source이(가) null인 경우

시퀀스의 요소를 TResult 형식으로 캐스팅할 수 없는 경우

예제

다음 코드 예제에서는 표준 쿼리 연산자를 사용할 Cast<TResult>(IEnumerable) 수 있도록 하는 방법을 보여 줍니다 ArrayList.

System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("mango");
fruits.Add("apple");
fruits.Add("lemon");

IEnumerable<string> query =
    fruits.Cast<string>().OrderBy(fruit => fruit).Select(fruit => fruit);

// The following code, without the cast, doesn't compile.
//IEnumerable<string> query1 =
//    fruits.OrderBy(fruit => fruit).Select(fruit => fruit);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}

// This code produces the following output:
//
// apple
// lemon
// mango
' Create an ArrayList and add items to it.
Dim fruits As New System.Collections.ArrayList()
fruits.Add("mango")
fruits.Add("apple")
fruits.Add("lemon")

' Call Cast(Of String) to cast the ArrayList elements to strings.
Dim query As IEnumerable(Of String) =
fruits.Cast(Of String)().OrderBy(Function(fruit) fruit).Select(Function(fruit) fruit)

'' The following code, without the cast, doesn't compile.
'Dim query As IEnumerable(Of String) = _
'    fruits.OrderBy(Function(fruit) fruit).Select(Function(fruit) fruit)

Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next

' Display the output.
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' apple
' lemon
' mango

설명

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

Cast<TResult>(IEnumerable) 메서드를 사용하면 필요한 형식 정보를 제공하여 제네릭이 아닌 컬렉션에서 표준 쿼리 연산자를 호출할 수 있습니다. 예를 들어 ArrayList 구현IEnumerable<T>하지 않지만 개체를 ArrayList 호출 Cast<TResult>(IEnumerable) 하면 표준 쿼리 연산자를 사용하여 시퀀스를 쿼리할 수 있습니다.

요소를 형식 TResult으로 변환할 수 없는 경우 이 메서드는 .를 InvalidCastExceptionthrow합니다.

이 메서드 IEnumerable의 소스 시퀀스는 요소에 컴파일 시간 정적 형식 object이 있다는 것을 의미합니다. 이 메서드에서 수행하는 유일한 형식 변환은 참조 변환 및 언박싱 변환입니다. 컬렉션에 있는 요소의 런타임 형식은 대상 형식과 일치해야 합니다. 또는 값 형식의 경우 요소의 런타임 형식은 대상 형식의 boxing 변환의 결과여야 합니다. 다른 숫자 형식 간의 변환 형식과 같은 다른 변환 형식은 허용되지 않습니다.

형식TResult으로 변환할 수 있는 요소만 가져오려면 대신 메서드Cast<TResult>(IEnumerable)OfType 사용합니다.

쿼리 식에서 명시적으로 형식화된 반복 변수는 호출로 Cast<TResult>(IEnumerable)변환됩니다. 이 예제에서는 명시적으로 형식화된 범위 변수에 대한 구문을 보여줍니다.

from int i in objects
From i As Integer In objects

쿼리 절을 select 사용하여 암시적 숫자 변환과 같은 다른 변환 형식을 수행합니다. 다음 예제에서는 메서드와 select 문을 모두 Cast 사용하여 박스형 정수 시퀀스를 double 시퀀스로 변환합니다.

IEnumerable sequence = Enumerable.Range(0, 10);
var doubles = from int item in sequence
                select (double)item;
Dim sequence As IEnumerable = Enumerable.Range(0, 10)
Dim doubles = From item As Integer In sequence
                Select CType(item, Double)

적용 대상

추가 정보