How do nullable types work in C #?

What is the difference behind the scenes between 'int?' and 'int'? Is 'int?' somehow a reference type?

+18
c #
Sep 21 '08 at 4:15
source share
3 answers

? wraps the value type (T) in a Nullable <T> structure:

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

+33
21 sept. '08 at 4:16
source share

In addition to "int?" being a shortcut to "Nullable", was the infrastructure included in the CLR to implicitly and silently convert between "int"? and "int". It also means that any operation in the box will implicitly put the actual value (i.e. it is impossible to insert Nullable as Nullable, it always results in either a size in a square T or a null object).

I ran into many of these problems when trying to create Nullable when you don't know T at compile time (you only know this at runtime). http://bradwilson.typepad.com/blog/2008/07/creating-nullab.html

+5
Sep 21 '08 at 4:24
source share

For one of the best β€œbackstage” discussions about Nullable types, you should look at the CLR Via C # by Jeffrey Richter.

Chapter 18 is a detailed discussion of null types. This book is also great for many other areas of the embedded .NET CLR.

+4
21 Sep '08 at 4:34
source share



All Articles