I analyzed the IL code for the following three methods:
generic<class T> where T : value class, System::ValueType static void MyMethod(T arg) { } generic<typename T> where T: value class static void MyMethod2(T arg) { } generic<typename T> where T: ValueType static void MyMethod3(T arg) { }
Relevant IL code that I parsed with .NET-Reflector:
.method public hidebysig static void MyMethod<valuetype ([mscorlib]System.ValueType).ctor T> (!!T arg) cil managed { } .method public hidebysig static void MyMethod2<valuetype .ctor T>(!!T arg) cil managed { } .method public hidebysig static void MyMethod3<([mscorlib]System.ValueType) T>(!!T arg) cil managed { }
This is the Nullable<T> IL declaration:
.class public sequential ansi serializable sealed beforefieldinit Nullable<valuetype (System.ValueType) .ctor T> extends System.ValueType
As you can clearly see, only the first restriction of the method corresponds to 100% with Nullable<T> . (The Btw: value class seems to imply a standard constructor). However, why the compiler creates different IL code for the (semantically) the same limitations remains a mystery. I will ask the Microsoft C ++ / CLI Guru for more information.
Simon
source share