Code inspection: Replace with '.OfType<T>()'
This inspection identifies LINQ Where() calls that filter elements by type using an is expression, followed by a Cast<T>() call. These combined operations can be replaced with a single call to OfType<T>(), which is more concise and readable.
public void Sample(object[] objects)
{
_ = objects.Where(a => a is List<string?>).Cast<List<string>>();
}
public void Sample(object[] objects)
{
_ = objects.OfType<List<string>>();
}
The quick-fix replaces the Where().Cast<T>() chain with a single OfType<T>() call.
11 March 2026