OfType in Linq

Gopesh9
Posted by Gopesh9 under LINQ category on | Points: 40 | Views : 1218
A sample program showing how you can use OfType in Linq

class Program
{
public static void Main(string[] args)
{
object[] values = { "Hi", 2, 10, 54.35, "Hello", "Bye", 35.05 };
IEnumerable stringQuery = values.OfType();
IEnumerable intQuery = values.OfType();
IEnumerable doubleQuery = values.OfType();

Console.WriteLine("Retreiving String Data");
foreach (string str in stringQuery)
{
Console.WriteLine(str);
}

Console.WriteLine("\nRetreiving Integer Data");
foreach (int i in intQuery)
{
Console.WriteLine(i);
}

Console.WriteLine("\nRetreiving Double Data");
foreach (double f in doubleQuery)
{
Console.WriteLine(f);
}

}
}
}

OutPut:
Retreiving String Data
Hi
Hello
Bye

Retreiving Integer Data
2
10

Retreiving Double Data
54.35
35.05

Comments or Responses

Login to post response