Sunday, September 22, 2019 02:28

Nullable variable types

You may have noticed already that some variables can have a default value of nullwhile others can’t. Well, that is not entirely true. Nullable variable types refers to exactly this situation: creating a specific wrapper around the value types (types that cannot be null), that allow them to store data with a null value.

This way, you can make these variables store both normal values and the special value that is null. In this case, we say nullable variable types can store an optional value.

There are two ways in which we can declare a nullable variable type:

Both of the above declarations are equivalent. Obviously, the easiest one is the second option, adding a question mark after the variable type.

Nullable variable types are used for storing information, which is not mandatory. For example, if we want to store data for a student such as the first name and last name as mandatory and age as not required, we can use type int? for the age variable:

Comments

comments

Tags: , ,

Leave a Reply