Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / gtest-134.cs
1 // sestoft@dina.kvl.dk * 2004-08
2
3 using System;
4
5 class MyTest {
6   public static void Main(String[] args) {
7     Foo<int?> fni1 = new Foo<int?>(null);
8     Console.WriteLine(fni1.Fmt());
9     Foo<int?> fni2 = new Foo<int?>(17);
10     Console.WriteLine(fni2.Fmt());
11     Foo<int> fi = new Foo<int>(7);
12     Console.WriteLine(fi.Fmt());
13     Foo<String> fs1 = new Foo<String>(null);
14     Console.WriteLine(fs1.Fmt());
15     Foo<String> fs2 = new Foo<String>("haha");
16     Console.WriteLine(fs2.Fmt());
17   }
18 }
19
20 class Foo<T> {
21   T x;
22   public Foo(T x) { 
23     this.x = x;
24   }
25   
26   // This shows how to deal with tests for null in a generic setting
27   // where null may mean both `null reference' and `null value of a
28   // nullable type'.  Namely, the test (x == null) will always be
29   // false if the generic type parameter t is instantiated with a
30   // nullable type.  Reason: the null literal will be considered a
31   // null reference and x will be boxed if a value type, and hence the
32   // comparison will be false...
33
34   public String Fmt() {
35     if (x != null)
36       return x.ToString();
37     else
38       return "null";
39   }  
40 }