How to determine whether T is a value type or reference class in generic?

[The following answer does not check the static type of T but the dynamic type of obj. This is not exactly what you asked for, but since it might be useful for your problem anyway, I'll keep this answer for reference.]

All value types (and only those) derive from System.ValueType. Thus, the following condition can be used:

if (obj is ValueType) {
    ...
} else {
    ...
}

You can use the typeof operator with generic types, so typeof(T) will get the Type reference corresponding to T, and then use the IsValueType property:

if (typeof(T).IsValueType)

Or if you want to include nullable value types as if they were reference types:

// Only true if T is a reference type or nullable value type
if (default(T) == null)

Tags:

C#

.Net

Generics