Enumerable.Cast(IEnumerable) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Casts the elements of an IEnumerable to the specified type.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^ Cast(System::Collections::IEnumerable ^ source);
public static System.Collections.Generic.IEnumerable Cast(this System.Collections.IEnumerable source);
static member Cast : System.Collections.IEnumerable -> seq<'Result>
Public Function Cast(Of TResult) (source As IEnumerable) As IEnumerable(Of TResult)
Type Parameters
- TResult
The type to cast the elements of source to.
Parameters
- source
- IEnumerable
The IEnumerable that contains the elements to be cast to type TResult.
Returns
An IEnumerable
Exceptions
source is null.
An element in the sequence cannot be cast to type TResult.
Examples
The following code example demonstrates how to use Cast
System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("mango");
fruits.Add("apple");
fruits.Add("lemon");
IEnumerable query =
fruits.Cast().OrderBy(fruit => fruit).Select(fruit => fruit);
// The following code, without the cast, doesn't compile.
//IEnumerable 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
Remarks
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. 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 C# or For Each in Visual Basic.
The Cast
If an element cannot be converted to type TResult, this method throws a InvalidCastException.
The source sequence for this method is IEnumerable, which means the elements have the compile-time static type of object. The only type conversions that are performed by this method are reference conversions and unboxing conversions. 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.
To obtain only those elements that can be converted to type TResult, use the OfType method instead of Cast
In a query expression, an explicitly typed iteration variable translates to an invocation of Cast
from int i in objects
From i As Integer In objects
Use the select clause of a query to perform other conversion types, like the implicit numeric conversions. 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)