X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FSystem%2FNullable.cs;h=b8ac671d69978cf8fbb0eb06db8344be9e803a99;hb=e2b2d181084848f3c5dde2788370db1b79893c69;hp=89fd585df5cfb330716a17ebb7e137abd8cbbd96;hpb=a3ea7ceb4d4f5e2cb8ea421313e8939640fb898c;p=mono.git diff --git a/mcs/class/corlib/System/Nullable.cs b/mcs/class/corlib/System/Nullable.cs index 89fd585df5c..b8ac671d699 100644 --- a/mcs/class/corlib/System/Nullable.cs +++ b/mcs/class/corlib/System/Nullable.cs @@ -1,7 +1,8 @@ // -// System.Nullable +// System.Nullable.cs // // Martin Baulig (martin@ximian.com) +// Marek Safar (marek.safar@gmail.com) // // (C) 2004 Novell, Inc. // @@ -30,32 +31,43 @@ // using System.Reflection; -#if NET_2_0 using System.Collections.Generic; -#endif using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Diagnostics; -#if NET_2_0 -namespace System { +namespace System +{ + [ComVisible (true)] public static class Nullable { - public static int Compare (Nullable left, Nullable right) where T: struct + +#if NET_2_1 + [ComVisible (false)] +#endif + public static int Compare (T? n1, T? n2) where T: struct { - IComparable icomparable = left.value as IComparable; - if (icomparable == null) - throw new ArgumentException ("At least one object must implement IComparable."); - if (left.has_value == false && right.has_value == false) - return 0; - if (!left.has_value) - return -1; - if (!right.has_value) - return 1; + if (n1.has_value) { + if (!n2.has_value) + return 1; - return icomparable.CompareTo (right.value); + return Comparer.Default.Compare (n1.value, n2.value); + } + + return n2.has_value ? -1 : 0; } - public static bool Equals (Nullable value1, Nullable value2) where T: struct +#if NET_2_1 + [ComVisible (false)] +#endif + public static bool Equals (T? n1, T? n2) where T: struct { - return value1.Equals (value2); + if (n1.has_value != n2.has_value) + return false; + + if (!n1.has_value) + return true; + + return EqualityComparer.Default.Equals (n1.value, n2.value); } public static Type GetUnderlyingType (Type nullableType) @@ -70,6 +82,7 @@ namespace System { } [Serializable] + [DebuggerStepThrough] public struct Nullable where T: struct { #region Sync with runtime code @@ -108,14 +121,13 @@ namespace System { bool Equals (Nullable other) { - Nullable no = (Nullable) other; - if (no.has_value != has_value) + if (other.has_value != has_value) return false; if (has_value == false) return true; - return no.value.Equals (value); + return other.value.Equals (value); } public override int GetHashCode () @@ -128,15 +140,12 @@ namespace System { public T GetValueOrDefault () { - return GetValueOrDefault (default (T)); + return has_value ? value : default (T); } - public T GetValueOrDefault (T def_value) + public T GetValueOrDefault (T defaultValue) { - if (!has_value) - return def_value; - else - return value; + return has_value ? value : defaultValue; } public override string ToString () @@ -157,14 +166,19 @@ namespace System { return value.Value; } + // // These are called by the JIT - // Ironicly, the C# code is the same for these two, - // however, on the inside they do somewhat different things + // +#pragma warning disable 169 + // + // JIT implementation of box valuetype System.Nullable`1 + // static object Box (T? o) { - if (o == null) + if (!o.has_value) return null; - return (T) o; + + return o.value; } static T? Unbox (object o) @@ -173,6 +187,6 @@ namespace System { return null; return (T) o; } +#pragma warning restore 169 } } -#endif