//
-// System.Nullable
+// System.Nullable.cs
//
// Martin Baulig (martin@ximian.com)
+// Marek Safar (marek.safar@gmail.com)
//
// (C) 2004 Novell, Inc.
//
//
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<T> (Nullable<T> left, Nullable<T> right) where T: struct
+
+#if NET_2_1
+ [ComVisible (false)]
+#endif
+ public static int Compare<T> (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<T>.Default.Compare (n1.value, n2.value);
+ }
+
+ return n2.has_value ? -1 : 0;
}
- public static bool Equals<T> (Nullable <T> value1, Nullable<T> value2) where T: struct
+#if NET_2_1
+ [ComVisible (false)]
+#endif
+ public static bool Equals<T> (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<T>.Default.Equals (n1.value, n2.value);
}
public static Type GetUnderlyingType (Type nullableType)
{
if (nullableType == null)
throw new ArgumentNullException ("nullableType");
- if (nullableType.IsGenericType && nullableType.GetGenericTypeDefinition () == typeof (Nullable<>))
- return nullableType.GetGenericArguments ()[0];
- else
- return null;
+
+ return nullableType.IsGenericType && !nullableType.IsGenericTypeDefinition && nullableType.GetGenericTypeDefinition () == typeof(Nullable<>) ?
+ nullableType.GetGenericArguments () [0] : null;
}
}
[Serializable]
+ [DebuggerStepThrough]
public struct Nullable<T> where T: struct
{
#region Sync with runtime code
bool Equals (Nullable<T> other)
{
- Nullable<T> no = (Nullable<T>) 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 ()
public T GetValueOrDefault ()
{
- return GetValueOrDefault (default (T));
+ return value;
}
- 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 ()
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)
return null;
return (T) o;
}
+#pragma warning restore 169
}
}
-#endif