Use Linq OfType to get value of specific type in CSharp

Description

The following code shows how to use Linq OfType to get value of specific type.

Example


using System;//from ww  w.j  av a 2  s .c o m
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {
        object[] numbers = { null, 1.0, "two", 3, 4.0f, 5, "six", 7.0 };
        var doubles = numbers.OfType<double>();
        Console.WriteLine("Numbers stored as doubles:");
        foreach (var d in doubles) {
            Console.WriteLine(d);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ