Nullable Types
There are two kinds of types:
- Value types such as
int
,double
andbool
. - Reference types such as
string
andList<int>
. All classes define reference types.
Reference types can be set to null
, but value types can’t:
int x = null; // Error
Sometimes, however, the ability to set value types to null
can come in handy. In Java, in order to have a “nullable int
”,
you would use an Integer
. C# instead offers nullable types:
simply add ?
to a value type. For example,
int? x = null;