I\'d like to have an integer variable which can be set to null and don\'t want to have to use the int? myVariable
syntax. I tried using int
and
You will have to use a nullable type, i.e:
Nullable or int?
The purpose of nullable type is, to enable you to assign null to a value type.
From MSDN:
Nullable types address the scenario where you want to be able to have a primitive type with a null (or unknown) value. This is common in database scenarios, but is also useful in other situations.
In the past, there were several ways of doing this:
- A boxed value type. This is not strongly-typed at compile-time, and involves doing a heap allocation for every type.
- A class wrapper for the value type. This is strongly-typed, but still involves a heap allocation, and the you have to write the wrapper.
- A struct wrapper that supports the concept of nullability. This is a good solution, but you have to write it yourself.