11

The definition of Nullable<T> is:

[SerializableAttribute]
public struct Nullable<T> where T : struct, new()

The constraint where T : struct implies that T can only be a value type. So I very well understand that I cannot write:

Nullable<string> a; //error. makes sense to me

Because string is a reference type, not a value type. But I don't really understand why can't I write

Nullable<Nullable<int>> b; //error. but why?

Why is it not allowed? After all, Nullable<int> is a value-type, and therefore, it can be type argument to Nullablle<T>.

When I compiled it on ideone, it gives this error (ideone):

error CS0453: The type 'int?' must be a non-nullable value type in order to use it as type parameter 'T' in the generic type or method 'System.Nullable' Compilation failed: 1 error(s), 0 warnings

BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
Nawaz
  • 341,464
  • 111
  • 648
  • 831
  • 2
    Sorry, my answer is wrong. The statement `new Nullable();` compiles (resulting in an `int?` with a value of null), although the default constructor isn't documented. – BoltClock Sep 29 '11 at 12:24
  • And yes, the answer is in the error that you should get when you try to compile your code. – BoltClock Sep 29 '11 at 12:26
  • @rudolf_franek: whatever that means, but my focus is on the type constraint, not on the semantic mean of the code. – Nawaz Sep 29 '11 at 12:30
  • @rudolf_franek: As I said, I'm focusing on the constraint type, according to which the code should compile, even if it semantically doesn't make sense. – Nawaz Sep 29 '11 at 12:34
  • @rudolf_franek: Sorry. your comments don't help in understanding the language here. – Nawaz Sep 29 '11 at 12:38

5 Answers5

10

Because it's in the C# spec (section 4.4.4):

If the constraint is the value type constraint (struct), the type A must satisfy one of the following:

  • A is a struct type or enum type, but not a nullable type. Note that System.ValueType and System.Enum are reference types that do not satisfy this constraint.
  • A is a type parameter having the value type constraint (§10.1.5).
Community
  • 1
  • 1
thecoop
  • 44,124
  • 18
  • 127
  • 185
  • 1
    Please expand on the spec quotation. I didn't completely understand that. I'm new to C#. – Nawaz Sep 29 '11 at 12:33
  • The `struct` generic type constraint you apply to classes is _specified_ as meaning 'all value & enum types apart from `Nullable`', specifically to stop this circularity. – thecoop Sep 29 '11 at 12:38
  • @thecoop: It's not a singularity. Were it not for the `struct` constraint on `Nullable`'s type parameter, one could usefully have a `Dictionary` include a method `Nullable `TryGetValue(TKey)` without having to use an `out` parameter. Given `Dictionary> myDict`, after `var it=myDict.TryGetValue("Fred");` if `it.HasValue` was false, that would mean no key was found; if `it.HasValue` was true but `it.Value.HasValue` was false, that would mean a null value was stored for key "Fred". The real difficulty stems from MS's decision to have... – supercat Mar 25 '15 at 16:26
  • ...a default-valued `Nullable` box as null rather than as a default-valued `Nullable`, which makes different kinds of empty nullables indistinguishable. – supercat Mar 25 '15 at 16:27
6

From section 4.1.10 of the C# language spec:

A non-nullable value type conversely is any value type other than System.Nullable<T> and its shorthand T? (for any T), plus any type parameter that is constrained to be a non-nullable value type (that is, any type parameter with a struct constraint). The System.Nullable<T> type specifies the value type constraint for T (§10.1.5), which means that the underlying type of a nullable type can be any non-nullable value type. The underlying type of a nullable type cannot be a nullable type or a reference type. For example, int?? and string? are invalid types.

Gabe
  • 82,547
  • 12
  • 135
  • 231
4

From §10.1.5 of the C# 4 spec:

The value type constraint specifies that a type argument used for the type parameter must be a non-nullable value type. All non-nullable struct types, enum types, and type parameters having the value type constraint satisfy this constraint. Note that although classified as a value type, a nullable type (§4.1.10) does not satisfy the value type constraint. A type parameter having the value type constraint cannot also have the constructor-constraint.

svick
  • 225,720
  • 49
  • 378
  • 501
2

As others have said, the spec prohibits this.

Digging deeper, it's worth realising that you can make your own struct that allows this pattern:

struct Nestable<T> where T : struct { /* ... */ }

new Nestable<Nestable<int>>(); // This works just fine

The prohibition of nested nullables can not be expressed using the type system available to you and me. It is only enforced by a special case in the compiler (CS0453).


Aside: The new() constraint shown in the question doesn't actually exist on System.Nullable<T>. new() constraints are prohibited when using the struct constraint.

CS0451: The 'new()' constraint cannot be used with the 'struct' constraint

All structs support default initialisation anyway.

Drew Noakes
  • 284,599
  • 158
  • 653
  • 723
  • According to the docs, there is no longer a `new()` constraint: https://referencesource.microsoft.com/#mscorlib/system/nullable.cs – Matthew Layton Sep 06 '17 at 13:09
2

This isn't exactly an answer, but just food for thought.

Round 1

Nullable<Nullable<int>> a;

error CS0453: The type 'int?' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable'

Intellisense hints... The name can be simplified


Round 2

Nullable<int?> a;

error CS0453: The type 'int?' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable'

Intellisense hints... The name can be simplified


Round 3

int?? a;

error CS1519: Invalid token '??' in class, struct, or interface member declaration

error CS1525: Invalid expression term '??'


Conclusion

int? is essentially just a short-hand evaluation of Nullable<int>, but there's no such thing as int?? which is the only way I can see of representing Nullable<Nullable<int>> in short-hand. Plus int?? borrows the null-coalescing operator, so I'm glad it's not possible because it looks dreadful. Imagine int????????????? a; How pointless.

Finally, since the reference source for Nullable does not yield any constraints for enforcing this, my guess is that this constraint was baked into the CLR as a special case when nullable value types were introduced into C#.

Community
  • 1
  • 1
Matthew Layton
  • 35,375
  • 44
  • 163
  • 278