2007-05-25 Jonathan Chambers <joncham@gmail.com>
[mcs.git] / tests / gtest-131.cs
blob2927b41eda3be55ec2e173302907c2280e75c210
1 //-- ex-nullable
3 using System;
5 class MyTest {
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 };
16 i2 += i1;
17 i2 += i4;
18 Console.WriteLine("i2 = {0}", i2); // 66 = 11+22+33
20 int sum = 0;
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++)
27 if (iarr[i] > 11)
28 Console.Write("[{0}] ", iarr[i]); // 22 33
29 Console.WriteLine();
31 for (int i=0; i<iarr.Length; i++)
32 if (iarr[i] != i1)
33 Console.Write("[{0}] ", iarr[i]); // 22 null 33 null
34 Console.WriteLine();
35 Console.WriteLine();
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");
39 Console.WriteLine();
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);