4 // Martin Baulig (martin@ximian.com)
5 // Marek Safar (marek.safar@gmail.com)
7 // (C) 2004 Novell, Inc.
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Reflection;
34 using System.Collections.Generic;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Diagnostics;
42 public static class Nullable {
47 public static int Compare<T> (T? n1, T? n2) where T: struct
53 return Comparer<T>.Default.Compare (n1.value, n2.value);
56 return n2.has_value ? -1 : 0;
62 public static bool Equals<T> (T? n1, T? n2) where T: struct
64 if (n1.has_value != n2.has_value)
70 return EqualityComparer<T>.Default.Equals (n1.value, n2.value);
73 public static Type GetUnderlyingType (Type nullableType)
75 if (nullableType == null)
76 throw new ArgumentNullException ("nullableType");
77 if (nullableType.IsGenericType && nullableType.GetGenericTypeDefinition () == typeof (Nullable<>))
78 return nullableType.GetGenericArguments ()[0];
86 public struct Nullable<T> where T: struct
88 #region Sync with runtime code
90 internal bool has_value;
93 public Nullable (T value)
95 this.has_value = true;
99 public bool HasValue {
100 get { return has_value; }
106 throw new InvalidOperationException ("Nullable object must have a value.");
112 public override bool Equals (object other)
115 return has_value == false;
116 if (!(other is Nullable<T>))
119 return Equals ((Nullable <T>) other);
122 bool Equals (Nullable<T> other)
124 if (other.has_value != has_value)
127 if (has_value == false)
130 return other.value.Equals (value);
133 public override int GetHashCode ()
138 return value.GetHashCode ();
141 public T GetValueOrDefault ()
146 public T GetValueOrDefault (T defaultValue)
148 return has_value ? value : defaultValue;
151 public override string ToString ()
154 return value.ToString ();
159 public static implicit operator Nullable<T> (T value)
161 return new Nullable<T> (value);
164 public static explicit operator T (Nullable<T> value)
170 // These are called by the JIT
172 #pragma warning disable 169
174 // JIT implementation of box valuetype System.Nullable`1<T>
176 static object Box (T? o)
184 static T? Unbox (object o)
190 #pragma warning restore 169