C# Enumerable Cast
Description
Casts the elements of an IEnumerable to the specified type.
Syntax
public static IEnumerable<TResult> Cast<TResult>(
this IEnumerable source
)
Parameters
TResult
- The type to cast the elements of source to.source
- The IEnumerable that contains the elements to be cast to type TResult.
Example
The following code example demonstrates how to use Cast(IEnumerable) to enable the use of the standard query operators on an ArrayList.
/*from ww w . j a v a2s. c om*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
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);
foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
}
}
The code above generates the following result.