Search content within the blog

Thursday, May 28, 2009

Using Cast in LINQ

Here is an example showing how to use cast function in LINQ using method syntax

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

No comments:

Post a Comment