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;
40 public static class Nullable {
41 public static int Compare<T> (Nullable<T> left, Nullable<T> right)
43 IComparable icomparable = left.value as IComparable;
44 if (icomparable == null)
45 throw new ArgumentException ("At least one object must implement IComparable.");
46 if (left.has_value == false && right.has_value == false)
53 return icomparable.CompareTo (right.value);
56 public static bool Equals<T> (Nullable <T> value1, Nullable<T> value2)
58 return value1.Equals (value2);
61 public static Nullable<T> FromObject<T> (object value)
64 throw new ArgumentException ("Object type can not be converted to target type.");
66 return new Nullable<T> ((T) value);
69 public static T GetValueOrDefault<T>(Nullable<T> value)
71 return GetValueOrDefault<T> (value, default (T));
74 public static T GetValueOrDefault<T> (Nullable<T> value, T defaultValue)
82 public static bool HasValue<T> (Nullable <T> value)
84 return value.has_value;
87 public static object ToObject<T> (Nullable<T> value)
92 return (object)value.value;
96 public struct Nullable<T> : IComparable, INullableValue
99 internal bool has_value;
101 public Nullable (T value)
104 this.has_value = false;
106 this.has_value = true;
111 public bool HasValue {
112 get { return has_value; }
118 throw new InvalidOperationException ("Nullable object must have a value.");
124 object INullableValue.Value {
126 return (object)Value;
131 public int CompareTo (Nullable<T> other)
133 return Nullable.Compare<T> (this, other);
137 public int CompareTo (object other)
139 if (!(other is Nullable<T>))
140 throw new ArgumentException ("Object type can not be converted to target type.");
142 return Nullable.Compare<T> (this, (Nullable<T>) other);
145 public override bool Equals (object other)
147 if (!(other is Nullable<T>))
150 return Equals ((Nullable <T>) other);
153 public bool Equals (Nullable<T> other)
155 if (other.has_value != has_value)
158 if (has_value == false)
161 return other.value.Equals (value);
165 public static Nullable<T> FromObject (object value)
167 return Nullable.FromObject<T> (value);
170 public override int GetHashCode ()
175 return value.GetHashCode ();
179 public T GetValueOrDefault ()
181 return Nullable.GetValueOrDefault<T> (this, default (T));
185 public T GetValueOrDefault (T def_value)
187 return Nullable.GetValueOrDefault<T> (this, def_value);
190 public object ToObject ()
195 return (object)value;
198 public override string ToString ()
201 return value.ToString ();
206 public static implicit operator Nullable<T> (T value)
208 return new Nullable<T> (value);
211 public static explicit operator T (Nullable<T> value)
216 public static bool operator == (Nullable<T> left, Nullable<T> right)
218 return left.Equals (right);
221 public static bool operator != (Nullable<T> left, Nullable<T> right)
223 return !left.Equals (right);