2

Excuse me if its a silly question, I am trying to get a better understanding of Nullable types in .Net.

From what i notice from Microsoft source code (using ReSharper), I understand that Nullable is a struct and T needs to be a struct

public struct Nullable<T> where T : struct

Now, I tried to do something like this

public struct CustomNullable<T> where T : struct
{
}
public class CustomNullableClass<T> where T : struct
{
}

And I get an error when I compile:

  Nullable<int?> a = null;
  Nullable<Nullable<int>> a1 = null;

For the above mentioned code I get an error 'Only non-nullable value types could be underlying of System.Nullable', but how is this enforced in the Nullable type ?

And for

   CustomNullable<int?> a2 = null;
   CustomNullableClass<int?> a3 = null;

I get an error 'The type System.Nullable must be non-nullable value type in order to use it as parameter T '.

I am bit confused now, can some one help me understand whats going on or have I not understood something ?

Thanks a lot in advance.

EDIT: If structs are value types and value types can't be null, how can a Nullable be a struct?

Credit : spender

RaM
  • 1,096
  • 10
  • 24
  • 1
    I'm sure there is a duplicate but I can't find it. Edit: done! – Soner Gönül Jul 07 '14 at 14:02
  • 1
    Why do you want a nullable nullable type? – Michael McGriff Jul 07 '14 at 14:02
  • 1
    Wouldn't a better question be, if structs are value types and value types can't be null, how can a `Nullable` be a struct? – spender Jul 07 '14 at 14:03
  • I dont want to make a nullable nullable type, I am just trying to understand, playing about really – RaM Jul 07 '14 at 14:04
  • 1
    See here also: http://stackoverflow.com/questions/12476590/why-is-nullablet-nullable-why-it-cannot-be-reproduced?rq=1 – spender Jul 07 '14 at 14:07
  • 3
    @spender Actually, `Nullable` isn't really capable of being `null`, it "just" has a special state that when compared to `null` produces `true`. This is nicely evident through the fact that on a `null` `Nullable` you can still retrieve the property `HasValue`. Interestingly, once you *box* the nullable `null` value, it becomes *actual* `null` and calling any method on that results in `NullReferenceException` - yet another hack in the type system (comment in reference source: `we have special type system support that says a a boxed Nullable can be used where a boxed is used`). – Luaan Jul 07 '14 at 14:11
  • @Luuaan: I knew that, but it seemed like a pertinent question to get the OP to question more about the magical qualities of a `Nullable`. Thanks for stepping up to the mark :) – spender Jul 07 '14 at 14:44

2 Answers2

9

Actually, it's an internal hack somewhere in the C# compiler (guessing). You cannot replicate it in your own classes. If you make your own type, using the exact same IL, it will not enforce that additional hidden constraint.

Luaan
  • 59,957
  • 7
  • 91
  • 109
4

There is special compiler support specifically for Nullable. You're not capable of reproducing a number of different possible behaviors of Nullable with a custom struct. One of those behaviors is the one that you've mentioned here, that Nullable doesn't meet the generic constraint for a struct despite being a struct.

Servy
  • 197,813
  • 25
  • 319
  • 428