The Labs \ Source Viewer \ SSCLI \ System \ Nullable

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. using System;
  16. namespace System
  17. {
  18.     using System.Globalization;
  19.     using System.Reflection;
  20.     using System.Collections.Generic;
  21.     using System.Runtime.CompilerServices;
  22.     using System.Security;
  23.    
  24.     // Warning, don't put System.Runtime.Serialization.On*Serializ*Attribute
  25.     // on this class without first fixing ObjectClone::InvokeVtsCallbacks
  26.     // Also, because we have special type system support that says a a boxed Nullable<T>
  27.     // can be used where a boxed<T> is use, Nullable<T> can not implement any intefaces
  28.     // at all (since T may not). Do NOT add any interfaces to Nullable!
  29.     //
  30.     [TypeDependencyAttribute("System.Collections.Generic.NullableComparer`1")]
  31.     [TypeDependencyAttribute("System.Collections.Generic.NullableEqualityComparer`1")]
  32.     [Serializable()]
  33.     public struct Nullable<T> where T : struct
  34.     {
  35.         private bool hasValue;
  36.         internal T value;
  37.        
  38.         public Nullable(T value)
  39.         {
  40.             this.value = value;
  41.             this.hasValue = true;
  42.         }
  43.        
  44.         public bool HasValue {
  45.             get { return hasValue; }
  46.         }
  47.        
  48.         public T Value {
  49.             get {
  50.                 if (!HasValue) {
  51.                     ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoValue);
  52.                 }
  53.                 return value;
  54.             }
  55.         }
  56.        
  57.         public T GetValueOrDefault()
  58.         {
  59.             return value;
  60.         }
  61.        
  62.         public T GetValueOrDefault(T defaultValue)
  63.         {
  64.             return HasValue ? value : defaultValue;
  65.         }
  66.        
  67.         public override bool Equals(object other)
  68.         {
  69.             if (!HasValue)
  70.                 return other == null;
  71.             if (other == null)
  72.                 return false;
  73.             return value.Equals(other);
  74.         }
  75.        
  76.         public override int GetHashCode()
  77.         {
  78.             return HasValue ? value.GetHashCode() : 0;
  79.         }
  80.        
  81.         public override string ToString()
  82.         {
  83.             return HasValue ? value.ToString() : "";
  84.         }
  85.        
  86.         public static implicit operator Nullable<T>(T value)
  87.         {
  88.             return new Nullable<T>(value);
  89.         }
  90.        
  91.         public static explicit operator T(Nullable<T> value)
  92.         {
  93.             return value.Value;
  94.         }
  95.        
  96.         // The following already obsoleted methods were removed:
  97.         // public int CompareTo(object other)
  98.         // public int CompareTo(Nullable<T> other)
  99.         // public bool Equals(Nullable<T> other)
  100.         // public static Nullable<T> FromObject(object value)
  101.         // public object ToObject()
  102.         // public string ToString(string format)
  103.         // public string ToString(IFormatProvider provider)
  104.         // public string ToString(string format, IFormatProvider provider)
  105.        
  106.         // The following newly obsoleted methods were removed:
  107.         // string IFormattable.ToString(string format, IFormatProvider provider)
  108.         // int IComparable.CompareTo(object other)
  109.         // int IComparable<Nullable<T>>.CompareTo(Nullable<T> other)
  110.         // bool IEquatable<Nullable<T>>.Equals(Nullable<T> other)
  111.     }
  112.    
  113.     [System.Runtime.InteropServices.ComVisible(true)]
  114.     public static class Nullable
  115.     {
  116.         [System.Runtime.InteropServices.ComVisible(true)]
  117.         public static int Compare<T>(Nullable<T> n1, Nullable<T> n2) where T : struct
  118.         {
  119.             if (n1.HasValue) {
  120.                 if (n2.HasValue)
  121.                     return Comparer<T>.Default.Compare(n1.value, n2.value);
  122.                 return 1;
  123.             }
  124.             if (n2.HasValue)
  125.                 return -1;
  126.             return 0;
  127.         }
  128.        
  129.         [System.Runtime.InteropServices.ComVisible(true)]
  130.         public static bool Equals<T>(Nullable<T> n1, Nullable<T> n2) where T : struct
  131.         {
  132.             if (n1.HasValue) {
  133.                 if (n2.HasValue)
  134.                     return EqualityComparer<T>.Default.Equals(n1.value, n2.value);
  135.                 return false;
  136.             }
  137.             if (n2.HasValue)
  138.                 return false;
  139.             return true;
  140.         }
  141.        
  142.         // If the type provided is not a Nullable Type, return null.
  143.         // Otherwise, returns the underlying type of the Nullable type
  144.         public static Type GetUnderlyingType(Type nullableType)
  145.         {
  146.             if (nullableType == null) {
  147.                 throw new ArgumentNullException("nullableType");
  148.             }
  149.             Type result = null;
  150.             if (nullableType.IsGenericType && !nullableType.IsGenericTypeDefinition) {
  151.                 // instantiated generic type only
  152.                 Type genericType = nullableType.GetGenericTypeDefinition();
  153.                 if (genericType == typeof(Nullable<>)) {
  154.                     result = nullableType.GetGenericArguments()[0];
  155.                 }
  156.             }
  157.             return result;
  158.         }
  159.        
  160.         // The following already obsoleted methods were removed:
  161.         // public static bool HasValue<T>(Nullable<T> value)
  162.         // public static T GetValueOrDefault<T>(Nullable<T> value)
  163.         // public static T GetValueOrDefault<T>(Nullable<T> value, T valueWhenNull)
  164.        
  165.         // The following newly obsoleted methods were removed:
  166.         // public static Nullable<T> FromObject<T>(object value)
  167.         // public static object ToObject<T>(Nullable<T> value)
  168.         // public static object Wrap(object value, Type type)
  169.         // public static object Unwrap(object value)
  170.     }
  171. }

Developer Fusion