LINQ OfType Query Operator

By Peter Bromberg

OfType accepts not only IEnumerable<T>-based sources, but also sources that are written against the non-parameterized IEnumerable interface that was present in version 1.0 of the .NET Framework. The OfType operator allows users to apply the standard query operators to classic .NET collections like this:

// "classic" cannot be used directly with query operators
IEnumerable
classic = new OlderCollectionType();

// "modern" can be used directly with query operators
IEnumerable
<object> modern = classic.OfType<object>();

LINQ OfType Query Operator  (805 Views)