Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about Nullable<T> constraints

Tags:

c#

.net

nullable

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.

like image 481
n535 Avatar asked Apr 07 '10 21:04

n535


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

like image 61
Thomas Levesque Avatar answered Nov 11 '22 05:11

Thomas Levesque


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.

like image 23
yellowstar Avatar answered Nov 11 '22 06:11

yellowstar


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?

like image 3
Sam Harwell Avatar answered Nov 11 '22 06:11

Sam Harwell