4 // Martin Baulig (martin@ximian.com)
6 // (C) 2004 Novell, Inc.
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection;
34 using System.Collections.Generic;
36 using System.Runtime.CompilerServices;
43 public static class Nullable
45 public static int Compare<T> (Nullable<T> left, Nullable<T> right)
47 IComparable icomparable = left.value as IComparable;
48 if (icomparable == null)
49 throw new ArgumentException ("At least one object must implement IComparable.");
50 if (left.has_value == false && right.has_value == false)
57 return icomparable.CompareTo (right.value);
60 public static bool Equals<T> (Nullable <T> value1, Nullable<T> value2)
62 return value1.Equals (value2);
65 public static Nullable<T> FromObject<T> (object value)
68 throw new ArgumentException ("Object type can not be converted to target type.");
70 return new Nullable<T> ((T) value);
73 public static T GetValueOrDefault<T>(Nullable<T> value)
75 return GetValueOrDefault<T> (value, default (T));
78 public static T GetValueOrDefault<T> (Nullable<T> value, T defaultValue)
86 public static bool HasValue<T> (Nullable <T> value)
88 return value.has_value;
91 public static object ToObject<T> (Nullable<T> value)
96 return (object)value.value;
100 [CLSCompliant(false)]
101 public struct Nullable<T> : IComparable, INullableValue
104 internal bool has_value;
106 public Nullable (T value)
109 this.has_value = false;
111 this.has_value = true;
116 public bool HasValue {
117 get { return has_value; }
123 throw new InvalidOperationException ("Nullable object must have a value.");
129 object INullableValue.Value {
131 return (object)Value;
136 public int CompareTo (Nullable<T> other)
138 return Nullable.Compare<T> (this, other);
142 public int CompareTo (object other)
144 if (!(other is Nullable<T>))
145 throw new ArgumentException ("Object type can not be converted to target type.");
147 return Nullable.Compare<T> (this, (Nullable<T>) other);
150 public override bool Equals (object other)
152 if (!(other is Nullable<T>))
155 return Equals ((Nullable <T>) other);
158 public bool Equals (Nullable<T> other)
160 if (other.has_value != has_value)
163 if (has_value == false)
166 return other.value.Equals (value);
170 public static Nullable<T> FromObject (object value)
172 return Nullable.FromObject<T> (value);
175 public override int GetHashCode ()
180 return value.GetHashCode ();
184 public T GetValueOrDefault ()
186 return Nullable.GetValueOrDefault<T> (this, default (T));
190 public T GetValueOrDefault (T def_value)
192 return Nullable.GetValueOrDefault<T> (this, def_value);
195 public object ToObject ()
200 return (object)value;
203 public override string ToString ()
205 return value.ToString ();
208 public static implicit operator Nullable<T> (T value)
210 return new Nullable<T> (value);
213 public static explicit operator T (Nullable<T> value)
218 public static bool operator == (Nullable<T> left, Nullable<T> right)
220 return left.Equals (right);
223 public static bool operator != (Nullable<T> left, Nullable<T> right)
225 return !left.Equals (right);