6 public static void Main(String
[] args
) {
7 Console
.WriteLine("Note that null prints as blank or []\n");
8 int? i1
= 11, i2
= 22, i3
= null, i4
= i1
+i2
, i5
= i1
+i3
;
9 // Values: 11 22 null 33 null
10 Console
.WriteLine("[{0}] [{1}] [{2}] [{3}] [{4}]", i1
, i2
, i3
, i4
, i5
);
11 int i6
= (int)i1
; // Legal
12 // int i7 = (int)i5; // Legal but fails at run-time
13 // int i8 = i1; // Illegal
15 int?[] iarr
= { i1, i2, i3, i4, i5 }
;
18 Console
.WriteLine("i2 = {0}", i2
); // 66 = 11+22+33
21 for (int i
=0; i
<iarr
.Length
; i
++)
22 sum
+= iarr
[i
] != null ? iarr
[i
].Value
: 0;
23 // sum += iarr[i] ?? 0;
24 Console
.WriteLine("sum = {0}", sum
); // 66 = 11+22+33
26 for (int i
=0; i
<iarr
.Length
; i
++)
28 Console
.Write("[{0}] ", iarr
[i
]); // 22 33
31 for (int i
=0; i
<iarr
.Length
; i
++)
33 Console
.Write("[{0}] ", iarr
[i
]); // 22 null 33 null
36 int?[] ivals
= { null, 2, 5 }
;
37 Console
.WriteLine("{0,6} {1,6} {2,6} {3,6} {4,-6} {5,-6} {6,-6} {7,-6}",
38 "x", "y", "x+y", "x-y", "x<y", "x>=y", "x==y", "x!=y");
40 foreach (int? x
in ivals
)
41 foreach (int? y
in ivals
)
42 Console
.WriteLine("{0,6} {1,6} {2,6} {3,6} {4,-6} {5,-6} {6,-6} {7,-6}",
43 x
, y
, x
+y
, x
-y
, (x
<y
), (x
>=y
), x
==y
, x
!=y
);