Syntax for declaring a variable as Nullable (C#)
Nullable
OR
boll? b = null
Declaring a variable as Nullable enables two key members which are “HasValue” and “Value”. HasValue allows you to check whether a value has been set or not and Value member is used to access the value. Here is simple example that will test whether a Nullable bool variable is set or not
Example 1: to demonstrate Nullable value type
private void button1_Click(object sender, EventArgs e)
{
bool? b = null;
b = true; // comment this line for null test
if (b.HasValue)
{
MessageBox.Show("The value of B is : " + b.Value);
}
else
{
MessageBox.Show("The value of B is not set : ");
}
}
No comments:
Post a Comment