C# OfType() Method

The OfType() method in C# is a LINQ extension method that filters a collection based on the type of its elements. It returns only the elements that match the specified type, making it useful for working with heterogeneous collections containing different data types.

Syntax

Following is the syntax for the OfType() method −

public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source)

The method can be used with both method syntax and query syntax −

// Method syntax
var result = collection.OfType<Type>();

// Query syntax
var result = from item in collection.OfType<Type>() select item;

Parameters

  • TResult − The type of elements to filter and return from the source collection.

  • source − The IEnumerable collection to filter.

Return Value

Returns an IEnumerable<TResult> containing only the elements from the source collection that are of the specified type.

OfType() Filtering Process Mixed Collection "Katie" 100 "Brad" 200 OfType<string>() String Elements "Katie" "Brad" OfType<int>() Integer Elements 100, 200

Using OfType() with Query Syntax

Example

using System;
using System.Linq;
using System.Collections;

public class Demo {
   public static void Main() {
      IList list = new ArrayList();
      list.Add("Katie");
      list.Add(100);
      list.Add(200);
      list.Add(300);
      list.Add(400);
      list.Add("Brad");
      list.Add(600);
      list.Add(700);

      var myStr = from a in list.OfType<string>() select a;
      var myInt = from a in list.OfType<int>() select a;
      
      Console.WriteLine("Strings...");
      foreach (var strVal in myStr) {
         Console.WriteLine(strVal);
      }
      Console.WriteLine("Integer...");
      foreach (var intVal in myInt) {
         Console.WriteLine(intVal);
      }
   }
}

The output of the above code is −

Strings...
Katie
Brad
Integer...
100
200
300
400
600
700

Using OfType() with Method Syntax

Example

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      List<object> mixedList = new List<object> {
         "Hello", 42, 3.14, "World", true, 100, "C#"
      };

      var strings = mixedList.OfType<string>().ToList();
      var numbers = mixedList.OfType<int>().ToList();
      var booleans = mixedList.OfType<bool>().ToList();
      var doubles = mixedList.OfType<double>().ToList();

      Console.WriteLine("Strings: " + string.Join(", ", strings));
      Console.WriteLine("Integers: " + string.Join(", ", numbers));
      Console.WriteLine("Booleans: " + string.Join(", ", booleans));
      Console.WriteLine("Doubles: " + string.Join(", ", doubles));
   }
}

The output of the above code is −

Strings: Hello, World, C#
Integers: 42, 100
Booleans: True
Doubles: 3.14

OfType() vs Cast() Method

OfType() Cast()
Filters elements by type and ignores incompatible types Attempts to cast all elements and throws exception on failure
Returns only elements that match the specified type Returns all elements after casting or throws exception
Safe - no exceptions for type mismatches Can throw InvalidCastException

Example

using System;
using System.Linq;
using System.Collections;

public class Demo {
   public static void Main() {
      ArrayList list = new ArrayList { "Text", 123, "Another", 456 };

      // OfType() - safe filtering
      var strings = list.OfType<string>();
      Console.WriteLine("OfType() result:");
      foreach (var item in strings) {
         Console.WriteLine(item);
      }

      // Count total elements vs filtered elements
      Console.WriteLine($"Original count: {list.Count}");
      Console.WriteLine($"String count: {strings.Count()}");
   }
}

The output of the above code is −

OfType() result:
Text
Another
Original count: 4
String count: 2

Conclusion

The OfType() method provides a safe way to filter collections by type, returning only elements that match the specified type. It's particularly useful when working with non-generic collections like ArrayList or mixed-type collections, allowing you to extract specific types without throwing exceptions for incompatible elements.

Updated on: 2026-03-17T07:04:35+05:30

716 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements