Then Enumerable.cs provides extension method OfType(). It can be used to select elements in an IEnumerable based on the type.
Arguments: None (Has Generic Argument)
Operates On: IEnumerable
Example: We have a list of objects, containing integer, float, string and character items. The OfType() method can be used to get elements which are integer.
IList<object> list = new List<object>();
In order to select the elements which are int – the following code can be used.
var result = list.OfType<int>();
Here the OfType() method takes an int generic argument.
The output of the above code will be:
In the above code we are having the following elements as inputs:
Extension Method Snapshot
You can find the snapshot of OfType () method usage as provided by Visual Studio.