C# LINQ OfType
last modified October 16, 2026
In this article we show how to filter elements of sequences by type using the
LINQ OfType method.
Language-Integrated Query (LINQ) is a domain-specific language for querying data from various data sources, including arrays, lists, XML files, or databases.
public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source);
The OfType method filters the elements of an
IEnumerable based on a specified type. The TResult
is the type to filter the elements of the sequence on. The source
is the sequence whose elements to filter. The method returns only the elements
that are of the given type or a type derived from it; null
elements are skipped.
C# LINQ OfType example
The following is a simple example with the OfType method.
object[] vals = ["falcon", 12, "sky", "new", "34", 11, 2.5];
var res = vals.OfType<int>();
foreach (var e in res)
{
Console.WriteLine(e);
}
Console.WriteLine("-----------------------");
var res2 = vals.OfType<string>();
foreach (var e in res2)
{
Console.WriteLine(e);
}
We define an array of objects. In the array we have strings, integers, and a
double. With the help of the OfType method, we pick up the integers
and the strings separately.
var res = vals.OfType<int>();
Here we filter all integers from the array. The type is specified between the
angle brackets. The result is an IEnumerable<int>; the
elements are already of the int type. Note that the string "34"
is not an integer.
$ dotnet run 12 11 ----------------------- falcon sky new 34
C# LINQ OfType with derived types
In the next example, we work with an interface and record types.
List<IPerson> persons =
[
new User("John Doe"),
new Admin("Roger Roe"),
new User("Peter Smith"),
new SuperAdmin("Lucy Brown"),
new User("Peter Novotny")
];
var admins = from person in persons.OfType<Admin>()
select person;
Console.WriteLine(string.Join(Environment.NewLine, admins));
Console.WriteLine("---------------------------");
var users = persons.OfType<User>().Select(u => u.Name);
Console.WriteLine(string.Join(", ", users));
interface IPerson;
record User(string Name) : IPerson;
record Admin(string Name) : IPerson;
record SuperAdmin(string Name) : Admin(Name);
We define the IPerson interface and three record types. The
SuperAdmin record derives from the Admin record.
var admins = from person in persons.OfType<Admin>()
select person;
With a LINQ query expression, we select the admins. The super admin is
included, because it is also an Admin.
var users = persons.OfType<User>().Select(u => u.Name);
Since the filtered elements have the User type, we can access
their Name property directly, without a cast.
$ dotnet run
Admin { Name = Roger Roe }
SuperAdmin { Name = Lucy Brown }
---------------------------
John Doe, Peter Smith, Peter Novotny
C# LINQ OfType with non-generic collections
The OfType method works on the non-generic
IEnumerable interface. Therefore, it can be used to bring old
non-generic collections, such as ArrayList, into LINQ.
using System.Collections;
var list = new ArrayList { 4, "sky", 7, null, 2.5, 9 };
int sum = list.OfType<int>().Sum();
Console.WriteLine(sum);
var words = list.OfType<string>().Select(w => w.ToUpper());
Console.WriteLine(string.Join(", ", words));
We sum the integers of an ArrayList and uppercase its strings.
Other LINQ methods, such as Sum or Select, are not
available on ArrayList until we call OfType or
Cast. The null element is skipped.
$ dotnet run 20 SKY
C# LINQ Cast
There is a similar Cast method. It casts the elements of an
IEnumerable to the specified type. Unlike OfType, the
Cast method throws an InvalidCastException if an
element in the sequence cannot be cast to the given type. We use it when we know
that all elements have the given type.
object[] vals = ["falcon", 12, "sky", "new", "34", 11];
var res = vals.Cast<string>();
foreach (var e in res)
{
Console.WriteLine(e);
}
We use the Cast method on an array of objects. Like other LINQ
methods, Cast is lazy; the exception is thrown when the iteration
reaches the integer.
$ dotnet run falcon Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'. ...
Source
In this article we have presented the LINQ OfType method.
Author
List all C# tutorials.