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.

Answers


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.


EDIT

I was thinking again about that, and I did a little test :

var attributes = typeof(Nullable<>).GetGenericArguments()[0].GenericParameterAttributes;
Console.WriteLine(attributes);

This code produces the following output :

NotNullableValueTypeConstraint, DefaultConstructorConstraint

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?


Need Your Help

C# and VB.NET operator precedence

c# vb.net

Looking at the C# and VB.NET language specs I think it says that the logical Xor/Or/And operations have different precendence in the two languages. Am I reading that right? I was expecting them to ...

Type erasure prevents overloading, but allows overriding

java generics override overloading type-erasure

Could anyone explain to me why this is not allowed in Java? I have these three files (stripped and simplified for StackExchange):