2009-02-10 Jeffrey Stedfast <fejj@novell.com>
[mono-project/dkf.git] / mcs / tests / gtest-134.cs
blobdf5709472c92c72cd026e4f99614c241242976e0
1 // sestoft@dina.kvl.dk * 2004-08
3 using System;
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());
20 class Foo<T> {
21 T x;
22 public Foo(T x) {
23 this.x = x;
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...
34 public String Fmt() {
35 if (x != null)
36 return x.ToString();
37 else
38 return "null";