Enumerable.Cast(IEnumerable) Enumerable.Cast(IEnumerable) Enumerable.Cast(IEnumerable) Enumerable.Cast(IEnumerable) Method

定義

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

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 之項目轉換成的型別。The type to cast the elements of source to.

參數

source
IEnumerable IEnumerable IEnumerable IEnumerable

IEnumerable,其包含要轉換成型別 TResult 的項目。The IEnumerable that contains the elements to be cast to type TResult.

傳回

IEnumerable<TResult>

IEnumerable<T>,其中包含已轉型成指定之型別的每個來源序列項目。An IEnumerable<T> that contains each element of the source sequence cast to the specified type.

例外狀況

無法將序列中的項目轉換為型別 TResultAn element in the sequence cannot be cast to type TResult.

範例

下列程式碼範例示範如何使用Cast<TResult>(IEnumerable)若要啟用的標準查詢運算子在ArrayListThe following code example demonstrates how to use Cast<TResult>(IEnumerable) to enable the use of the standard query operators on an 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.
MsgBox(output.ToString())

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

備註

實作這個方法是使用延後的執行。This method is implemented by using deferred execution. 立即傳回值是儲存,才能執行此動作的所有資訊的物件。The immediate return value is an object that stores all the information that is required to perform the action. 這個方法所表示的查詢不會執行直到列舉物件可以藉由呼叫其GetEnumerator方法直接或透過foreachVisual C# 中或For EachVisual Basic 中。The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

Cast<TResult>(IEnumerable)方法可讓標準查詢運算子來叫用非泛型集合上提供必要的型別資訊。The Cast<TResult>(IEnumerable) method enables the standard query operators to be invoked on non-generic collections by supplying the necessary type information. 例如,ArrayList不會實作IEnumerable<T>,但藉由呼叫Cast<TResult>(IEnumerable)ArrayList物件時,標準查詢運算子可以再用來查詢的序列。For example, ArrayList does not implement IEnumerable<T>, but by calling Cast<TResult>(IEnumerable) on the ArrayList object, the standard query operators can then be used to query the sequence.

如果項目無法轉換為類型TResult,此方法會擲回InvalidCastExceptionIf an element cannot be converted to type TResult, this method throws a InvalidCastException.

來源序列,這個方法的IEnumerable,表示項目都有編譯時間靜態類型的objectThe source sequence for this method is IEnumerable, which means the elements have the compile-time static type of object. 這個方法所執行的唯一類型轉換為參考轉換和 unboxing 轉換。The only type conversions that are performed by this method are reference conversions and unboxing conversions. 集合中的項目執行階段型別必須符合目標類型,或實值型別,如果項目執行階段類型必須是目標類型的 boxing 轉換的結果。The runtime type of the elements in the collection must match the target type, or in the case of value types, the runtime type of elements must be the result of a boxing conversion of the target type. 其他的轉換類型,例如不同的數值型別,不允許。Other conversion types, such as those between different numeric types, are not allowed.

若要取得只可以轉換成輸入的項目才TResult,使用OfType方法,而非Cast<TResult>(IEnumerable)To obtain only those elements that can be converted to type TResult, use the OfType method instead of Cast<TResult>(IEnumerable).

在查詢運算式中,明確指定型別的的反覆運算變數會轉譯成的引動過程Cast<TResult>(IEnumerable)In a query expression, an explicitly typed iteration variable translates to an invocation of Cast<TResult>(IEnumerable). 此範例顯示具有明確類型的範圍變數的語法。This example shows the syntax for an explicitly typed range variable.

from int i in objects  
From i As Integer In objects  

使用select子句的查詢來執行其他的轉換類型,例如隱含數值轉換。Use the select clause of a query to perform other conversion types, like the implicit numeric conversions. 下列範例會使用這兩者Cast方法和select陳述式,以將一連串的 boxed 整數轉換成一連串的雙精度浮點數。The following example uses both the Cast method and a select statement to convert a sequence of boxed integers to a sequence of doubles.

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)

適用於

另請參閱