Nullable type in C#
We can declare a variable as nullable when we want to know that a value has been assigned to that variable or not. Declaring a variable as nullable enables the HasValue and Value members. We can use the HasValue property on the variable to check if the value has been assigned to the variable.
Example of Using Nullable
Let’s take an example of boolean variable. As we know the default value for a boolean variable is false. But we want that we should be able to know if user has selected true or false. So we define the bool variable as nullable like this:
Nullable<bool> testVariable= null;
Now we have the testVariable as a boolean variable having a value of null. Now when use the properties like this :
if(testVariable.HasValue) MessageBox.Show(String.Format("Value is {0}",testVariable.value ));