Testing if object is of generic type in C#

If you want to check if it’s an instance of a generic type:

return list.GetType().IsGenericType;

If you want to check if it’s a generic List<T>:

return list.GetType().GetGenericTypeDefinition() == typeof(List<>);

As Jon points out, this checks the exact type equivalence. Returning false doesn’t necessarily mean list is List<T> returns false (i.e. the object cannot be assigned to a List<T> variable).

Leave a Comment