Converts the elements of an IEnumerable to the specified type.
<ExtensionAttribute> _ Public Shared Function Cast(Of TResult) ( _ source As IEnumerable _ ) As IEnumerable(Of TResult)
Dim source As IEnumerable Dim returnValue As IEnumerable(Of TResult) returnValue = source.Cast()
public static IEnumerable<TResult> Cast<TResult>( this IEnumerable source )
[ExtensionAttribute] public: generic<typename TResult> static IEnumerable<TResult>^ Cast( IEnumerable^ source )
JScript does not support generic types or methods.
The type to convert the elements of source to.
source is nullNothingnullptra null reference (Nothing in Visual Basic).
An element in the sequence cannot be cast to type TResult.
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 Visual C# or For Each in Visual Basic.
The Cast<(Of <(TResult>)>)(IEnumerable) method enables the standard query operators to be invoked on non-generic collections by supplying the necessary type information. For example, ArrayList does not implement IEnumerable<(Of <(T>)>), but by calling Cast<(Of <(TResult>)>)(IEnumerable) on the ArrayList object, the standard query operators can then be used to query the sequence.
If an element cannot be cast to type TResult, this method will throw an exception. To obtain only those elements that can be cast to type TResult, use the OfType<(Of <(TResult>)>) method instead of Cast<(Of <(TResult>)>)(IEnumerable).
In a query expression, an explicitly typed iteration variable translates to an invocation of Cast<(Of <(TResult>)>)(IEnumerable). This example shows the syntax for an explicitly typed range variable.
from int i in objects
From i As Integer In objects
The following code example demonstrates how to use Cast<(Of <(TResult>)>)(IEnumerable) to enable the use of the standard query operators on an ArrayList.
' Create an ArrayList and add items to it. Dim fruits As New System.Collections.ArrayList() fruits.Add("apple") fruits.Add("mango") ' Call Cast(Of String) to cast the ArrayList elements to strings. ' Then call Select() to project the strings as the query result. Dim query As IEnumerable(Of String) = _ fruits.Cast(Of String)().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 ' mango
System.Collections.ArrayList fruits = new System.Collections.ArrayList(); fruits.Add("apple"); fruits.Add("mango"); IEnumerable<string> query = fruits.Cast<string>().Select(fruit => fruit); foreach (string fruit in query) { Console.WriteLine(fruit); } // This code produces the following output: // // apple // mango
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
Not only the description is misleading, but the method itself was actually implemented to *convert* not cast:
http://blogs.msdn.com/ed_maurer/archive/2008/02/16/breaking-change-in-linq-queries-using-explicitly-typed-range-variables.aspx
The behaviour is fixed in .net 3.5 sp1.