..
The term null is an interesting concept in the world of programming and is not synonymous with 'zero' or 'empty' but rather 'as undefined'. There is talk of nullabilità (nullability) to refer to the ability of a data type to accept and handle null values.
Every programmer often during the development of their applications is to have to do with the so-called value data types (value types) as int, double, bool, char, and datetime. These data types are basic but have one thing a bit awkward in some cases, that can not be null.
Fortunately, C # and the. NET Framework provides us with a very useful tool: nullable data types. These types of data can be used at any point in the code where there is a need to have a variable that can have values but can also have null value.
If the code of our application we write something like
boolTest bool = null; dataTest datetime = null;get errors at compile time. However, often presents the need to set a number, a boolean or a date to have null value and in these cases come to our rescue nullable data types. To make a data type in C # nullable everything you need to do is to put a question mark (?) At the end of each data type value.
Therefore, if we rewrite the two previous variables in the following way we will not have more errors at compile
bool? boolTest = null; datetime? dataTest = null;
Each variable of type Nullable exposes a property called Value, which allows you to get or set the value thereof. Another useful property of these types of data is HasValue, which returns the Boolean value true if the variable in question has a non-zero, false otherwise.
It 's always possible to convert a value type into a nullable type
dato1 bool = true; bool? dato2 = dato1but beware that to make the reverse conversion is necessary to cast the nullable type
dato1 = (bool) dato2;
When you put the question to follow a data type value, like int? Or decimal?, Basically the compiler translates it into struct Nullable Nullable <int> or <decimal>.
To check this just add to our code of a variable as Nullable <DateTime> and put a breakpoint in his correspondence.
Running the code and observing the Watch window to display as data type System.DateTime?.
| |
ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €. |
| |
OpenOffice (Ebook)
The open-source software for managing the office work. Just 25 €. |
| |
PHP (Course)
Full course for creating dynamic Web sites. From 49 €. |