namespace samples {
using System;
using NullableTypes;
sealed class MyApp {
static void Main() {
/*
*
* Definition samples
*
*/
// declaration
NullableInt32 ni;
// declaration and initialization
NullableInt32 nj = 320;
// declaration and initialization to Null
NullableInt32 nk = NullableInt32.Null;
/*
*
* Operators and expressions
*
*/
ni = nj * nk;
Console.WriteLine(" 1) {0}", ni); // -> NullableInt32.Null
nj *= 11;
Console.WriteLine(" 2) {0}", nj); // -> 3520
/*
*
* Assignments and conversions
*
*/
int i = 3200;
ni = i + 100; // an implicit conversion happen before the assignment
i = (int)ni; // this conversion can raise an exception if 'ni' is Null
Console.WriteLine(" 3) {0}", i); // -> 3300
/*
*
* Dealing with Null values
*
*/
nk = NullableInt32.Null; // set to null
nj = 2;
ni = nk / nj;
// Test for Null
Console.WriteLine(" 4) {0}", ni.IsNull); // -> bool.true
if (ni.IsNull)
Console.WriteLine(" if-true" ); // <-
else
Console.WriteLine(" if-NOT true");
// Comparison with a Null value return a NullableBoolean.Null,
// the 'if' will fail (as if false) with a NullableBoolean.Null
Console.WriteLine(" 6) {0}", (ni > nk)); // -> NullableBoolean.Null
if (ni > nk)
Console.WriteLine(" if-true");
else
Console.WriteLine(" if-NOT true"); // <-
// Comparison with a Null value return a NullableBoolean.Null,
// the 'if' will fail (as if false) with a NullableBoolean.Null
Console.WriteLine(" 7) {0}", (ni < nk)); // -> NullableBoolean.Null
if (ni < nk)
Console.WriteLine(" if-true");
else
Console.WriteLine(" if-NOT true"); // <-
// Getting the Value property raise an exception if the
// NullableInt32 variable is Null
try {
ni = NullableInt32.Null;
int k = ni.Value;
}
catch (NullableNullValueException ne) {
Console.WriteLine(" 8) {0}", ne.Message); // -> " Data is Null. This method or property cannot be called on Null values."
}
// An explicit conversion to int raise an exception if the
// NullableInt32 variable is Null
try {
ni = NullableInt32.Null;
int k = (int)ni;
}
catch (NullableNullValueException ne) {
Console.WriteLine(" 9) {0}", ne.Message); // -> " Data is Null. This method or property cannot be called on Null values."
}
return;
}
}
}