0

How is nullable type implemented by .NET ?

Nullable<int> x = 5;
int? x = 5;
GSerg
  • 76,472
  • 17
  • 159
  • 346
Brij
  • 11,731
  • 22
  • 78
  • 116
  • @JonSkeet, I looked at the duplicate question. If Nullable is a struct. Being a struct it is value type, then how come we are able to assign "null" to its instace ? int? = null – Brij Apr 30 '13 at 16:38

2 Answers2

0

Yes, is the same in .net

int? variable = 5;
0

So a Nullable<t> has two main properties: value and hasValue

Now if you have a value type (such as an int) - it can never have a null value, in the case of an int it would have an initial value of 0.

Since however value is a property, you can keep track separately of whether it's set to null or not, and if it's null yet you try to use the getter method of the value property, it can throw an exception.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62