Console.WriteLine(married); //---now OK---
Now married has a default value of false. There are times, though, when you do not know the marital status of a person, and the variable should be neither true nor false. In C#, you can declare value types to be
To make the married variable nullable, the above declaration can be rewritten in two different ways (all are equivalent):
Boolean? married = null;
//---or---
Nullable<Boolean> married = null;
The syntax T? (example, Boolean?) is shorthand for Nullable<T> (example, Nullable<Boolean>), where T is a type.
You read this statement as 'Nullable of Boolean.' The <> represents a generic type and will be discussed in more detail in Chapter 9.
In this case, married can take one of the three values: true, false, or null.
The following code snippet prints out 'Not Married':
Boolean? married = null;
if (married == true)
Console.WriteLine('Married');
else
Console.WriteLine('Not Married'); //---this will be printed---
That's because the if statement evaluates to false (married is currently null), so the else block executes. A much better way to check would be to use the following snippet:
if (married == true)
Console.WriteLine('Married');
else if (married==false)
Console.WriteLine('Not Married');
else
Console.WriteLine('Not Sure'); //---this will be printed---
Once a nullable type variable is set to a value, you can set it back to nothing by using null, as the following example shows:
married = true; //---set it to True---
married = null; //---reset it back to nothing---
To check the value of a nullable variable, use the HasValue property, like this:
if (married.HasValue) {
//---this line will be executed only
// if married is either true or false---
Console.WriteLine(married.Value);
}
You can also use the == operator to test against null, like the following:
if (married == null) {
//---causes a runtime error---
Console.WriteLine(married.Value);
}
But this results in an error because attempting to print out the value of a null variable using the Value property causes an exception to be thrown. Hence, always use the HasValue property to check a nullable variable before attempting to print its value.
When dealing with nullable types, you may want to assign a nullable variable to another variable, like this:
int? num1 = null;
int num2 = num1;
In this case, the compiler will complain because num1 is a nullable type while num2 is not (by default, num2 cannot take on a null value unless it is declared nullable). To resolve this, you can use the null ??). Consider the following example:
int? num1 = null;
int num2 = num1 ?? 0;
Console.WriteLine(num2); //---0---
In this statement, if num1 is null, 0 will be assigned to num2. If num1 is not null, the value of num1 will be assigned to num2, as evident in the following few statements:
num1 = 5;
num2 = num1 ?? 0;
Console.WriteLine(num2); //---5---
Reference Types
For reference types, the variable stores a reference to the data rather than the actual data. Consider the following:
Button btn1, btn2;
btn1 = new Button();
btn1.Text = 'OK';
btn2 = btn1;
Console.WriteLine('{0} {1}', btn1.Text, btn2.Text);
btn2.Text = 'Cancel';
Console.WriteLine('{0} {1}', btn1.Text, btn2.Text);
Here, you first declare two Button controls — btn1 and btn2. btn1's Text property is set to 'OK' and then btn2 is assigned btn1. The first output will be:
OK OK
When you change btn2's Text property to 'Cancel', you invariably change btn1's Text property, as the second output shows:
Cancel Cancel
That's because btn1 and btn2 are both pointing to the same Button object. They both contain a reference to that object instead of storing the value of the object. The declaration statement (Button btn1, btn2;) simply creates two variables that contain references to Button objects (in the example these two variables point to the same object).
To remove the reference to an object in a reference type, simply use the null keyword:
btn2 = null;
When a reference type is set to null, attempting to access its members results in a runtime error.
