Enumerable.Cast<TResult>(IEnumerable) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 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
該 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 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
此 Cast<TResult>(IEnumerable) 方法透過提供必要的型別資訊,使標準查詢運算子能在非通用集合中被調用。 例如,ArrayList不實作 IEnumerable<T>,但透過呼叫Cast<TResult>(IEnumerable)ArrayList物件,可以使用標準查詢運算子來查詢序列。
若元素無法轉換為型別 TResult,此方法會拋出一個 InvalidCastException。
此方法的來源序列為 IEnumerable,表示元素具有編譯時靜態型別。object 此方法唯一執行的型別轉換是參考轉換和開箱轉換。 集合中元素的執行型態必須與目標型別相符,否則在值型別的情況下,執行型態必須是目標型別的盒裝轉換結果。 其他轉換類型,例如不同數字類型之間的轉換,則不被允許。
若要只取得可轉換為型別 TResult的元素,請使用該 OfType 方法而非 Cast<TResult>(IEnumerable)。
在查詢表達式中,明確型別的迭代變數可轉換為 的呼叫 Cast<TResult>(IEnumerable)。 此範例展示了明確型別的範圍變數的語法。
from int i in objects
From i As Integer In objects
利用 select 查詢的子句來執行其他轉換類型,例如隱含的數字轉換。 以下範例結合了該 Cast 方法與一個 select 陳述,將一列盒狀整數轉換成一列雙重數。
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)