I'm under the same need, I want to create a method that should retrieve a List where T should be a primitive type like int, double, decimal, etc...
Based on this Microsoft documentation:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/unmanaged-types
Looks like the right approach is to use
where T : unmanaged
quoting:
A type is an unmanaged type if it's any of the following types:
sbyte, byte, short, ushort, int, uint, long, ulong, char, float,
double, decimal, or bool Any enum type Any pointer type Any
user-defined struct type that contains fields of unmanaged types only
and, in C# 7.3 and earlier, is not a constructed type (a type that
includes at least one type argument)
Also important quote:
Beginning with C# 7.3, you can use the unmanaged constraint to specify
that a type parameter is a non-pointer, non-nullable unmanaged type.
Beginning with C# 8.0, a constructed struct type that contains fields
of unmanaged types only is also unmanaged...
int
,float
etc. notInt32
,Int64
,Single
etc.. Althoughclass
denotes a ref type, "but not class type" is suggesting the distinction between unmanaged and managed primitives. – Bandeau