Merge pull request #350 from robwilkens/bug1089
[mono.git] / mcs / class / corlib / System / Nullable.cs
index bc86b446c5c93739201b8309f5dced4bcf9de24e..0d9d5928690bdb5ea239e6d901a134733ade3730 100644 (file)
@@ -1,5 +1,5 @@
 //
-// System.Nullable
+// System.Nullable.cs
 //
 // Martin Baulig (martin@ximian.com)
 // Marek Safar  (marek.safar@gmail.com)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System.Reflection;
 using System.Collections.Generic;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using System.Diagnostics;
 
 namespace System
 {
        [ComVisible (true)]
        public static class Nullable {
-               public static int Compare<T> (T? value1, T? value2) where T: struct
+
+#if NET_2_1
+               [ComVisible (false)]
+#endif
+               public static int Compare<T> (T? n1, T? n2) where T: struct
                {
-                       if (value1.has_value) {
-                               if (!value2.has_value)
+                       if (n1.has_value) {
+                               if (!n2.has_value)
                                        return 1;
 
-                               return Comparer<T>.Default.Compare (value1.value, value2.value);
+                               return Comparer<T>.Default.Compare (n1.value, n2.value);
                        }
                        
-                       return value2.has_value ? -1 : 0;
+                       return n2.has_value ? -1 : 0;
                }
 
-               public static bool Equals<T> (T? value1, T? value2) where T: struct
+#if NET_2_1
+               [ComVisible (false)]
+#endif
+               public static bool Equals<T> (T? n1, T? n2) where T: struct
                {
-                       if (value1.has_value != value2.has_value)
+                       if (n1.has_value != n2.has_value)
                                return false;
 
-                       if (!value1.has_value)
+                       if (!n1.has_value)
                                return true;
 
-                       return EqualityComparer<T>.Default.Equals (value1.value, value2.value);
+                       return EqualityComparer<T>.Default.Equals (n1.value, n2.value);
                }
 
                public static Type GetUnderlyingType (Type nullableType)
@@ -76,10 +82,14 @@ namespace System
        }
 
        [Serializable]
+       [DebuggerDisplay ("{DebuggerDisplay}")]
+       [DebuggerStepThrough]
        public struct Nullable<T> where T: struct
        {
                #region Sync with runtime code
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
                internal T value;
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
                internal bool has_value;
                #endregion
 
@@ -89,10 +99,19 @@ namespace System
                        this.value = value;
                }
 
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
+               string DebuggerDisplay {
+                       get {
+                               return has_value ? value.ToString () : null;
+                       }
+               }
+
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
                public bool HasValue {
                        get { return has_value; }
                }
 
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
                public T Value {
                        get { 
                                if (!has_value)
@@ -133,15 +152,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 ()
@@ -162,14 +178,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<T>
+               //
                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)
@@ -178,6 +199,6 @@ namespace System
                                return null;
                        return (T) o;
                }
+#pragma warning restore 169
        }
 }
-#endif