2004-04-07 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / corlib / System / Nullable.cs
1 //
2 // System.Nullable
3 //
4 // Martin Baulig (martin@ximian.com)
5 //
6 // (C) 2004 Novell, Inc.
7 //
8
9 using System.Reflection;
10 using System.Collections.Generic;
11 using System.Runtime.CompilerServices;
12
13 #if NET_1_2
14 namespace System
15 {
16         public struct Nullable<T> : IComparable<Nullable<T>>
17         {
18                 T value;
19                 bool has_value;
20
21                 public Nullable (T value)
22                 {
23                         this.value = value;
24                         this.has_value = true;
25                 }
26
27                 public bool HasValue {
28                         get { return has_value; }
29                 }
30
31                 public T Value {
32                         get { return value; }
33                 }
34
35                 public T GetValueOrDefault ()
36                 {
37                         if (has_value)
38                                 return value;
39                         return default (T);
40                 }
41
42                 public T GetValueOrDefault (T def_value)
43                 {
44                         if (has_value)
45                                 return value;
46                         else
47                                 return def_value;
48                 }
49
50                 public int CompareTo (Nullable<T> other)
51                 {
52                         if (!has_value && other.has_value)
53                                 return -1;
54                         else if (has_value && !other.has_value)
55                                 return 1;
56                         else if (!has_value && !other.has_value)
57                                 return 0;
58                         else if (value == other.value)
59                                 return 0;
60                         else
61                                 return 1;
62                 }
63         }
64 }
65 #endif