Confusion about Nullable<T> constraints
Greetings everybody. I am sorry, if this was already asked before (searched in vain) or is really very simple, but i just can't get it. The MSDN definition of a Nullable type, states, that it is defined in a following manner:
[SerializableAttribute] public struct Nullable<T> where T : struct, new()
So the question is quite straightforward: How is this definition possible? Or this is just a typo? Every value type already has a default constructor. Indeed, when i try to compile something like this, the compiler reasonably says, that it is illegal to apply both constraints at the same time, because the second one is implicitly included in a first one.
Thanks in advance.
I think it's just a mistake in the documentation. If you look at the Nullable<T> type in Reflector or using the "Go To Definition" command in VS, it shows only the struct constraint.
I was thinking again about that, and I did a little test :
var attributes = typeof(Nullable<>).GetGenericArguments().GenericParameterAttributes; Console.WriteLine(attributes);
This code produces the following output :
So according to reflection, the T in Nullable<T> does have the new() constraint... Which means that even though it's invalid in C#, it must be valid for the CLR.
So the documentation is both right and wrong : it's true that the T in Nullable<T> has the "default constructor" constraint, but the C# declaration it shows is wrong...
This is actually not very surprising, since the documentation is generated from assembly metadata (and XML comments of course). There must be a bug in the documentation generator...
C# requires that value types have default public constructors, but the CLR does not.
If you defined a type in a language that supported a struct definition of this type (I believe C++/CLI allows this) there would be ambiguity as to when it is called.
If you look at the IL disassembly, you can see that it does have a constructor constraint:
.class public sequential ansi serializable sealed beforefieldinit Nullable<valuetype (System.ValueType) .ctor T>
If the documentation is generated directly from the assembly metadata, maybe this is the cause?