LINQ OfType Filtering Operator

OfType comes in Filtering operator category. Here is list of all Query Operators.

LINQ OfType operator filter the objects from a collection based on Types. In a collection, we have multiple elements of different Types and we have to select only those objects from the collection that are of specific Type.

In the above C# example, we have one abstract class Course and two inherit classes FreeCourse and PaidCourse. We add all courses in List<Course> sequence. In the last, we select only paid courses from the collection by using OfType operator and passing PaidCourse Type as generic argument and select only free courses from the collection using OfType and passing FreeCourse as generic argument.