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

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).

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).

Type. IsValueType tells, naturally, if Type is a value type. Hence, typeof(T).IsValueType.

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 { ... }.

T is a type, not an expression, so you can't use is. Also, consider where T is exactly System. ValueType, which is a reference type... – Jon Skeet Sep 28 at 8:28 @JonSkeet: Thanks, good point, changed to obj is ValueType.

Since ValueType is an abstract class, obj cannot be exactly ValueType (of course, this assumes that obj is not null... I'll add that to my answer). – Heinzi Sep 28 at 8:30 That doesn't tell you about T. Consider SomeGenericMethod(null) - int?

Is a value type, but your condition won't match. – Jon Skeet Sep 28 at 8:32 @JonSkeet: I just realized the same thing and added it to my answer. As always, thanks for your feedback.

– Heinzi Sep 28 at 8:36 Of course I've also realised that it would report the wrong thing for SomeGenericMethod(1) as well - your edit explains that situation too. – Jon Skeet Sep 28 at 8:36.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions