mscorlib(4.0.0.0) API with additions
Nullable.cs
4 
5 namespace System
6 {
10  [NonVersionable]
11  [__DynamicallyInvokable]
12  public struct Nullable<T> where T : struct
13  {
14  private bool hasValue;
15 
16  internal T value;
17 
21  [__DynamicallyInvokable]
22  public bool HasValue
23  {
24  [NonVersionable]
25  [__DynamicallyInvokable]
26  get
27  {
28  return hasValue;
29  }
30  }
31 
35  [__DynamicallyInvokable]
36  public T Value
37  {
38  [__DynamicallyInvokable]
39  get
40  {
41  if (!hasValue)
42  {
43  ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NoValue);
44  }
45  return value;
46  }
47  }
48 
51  [NonVersionable]
52  [__DynamicallyInvokable]
53  public Nullable(T value)
54  {
55  this.value = value;
56  hasValue = true;
57  }
58 
61  [NonVersionable]
62  [__DynamicallyInvokable]
64  {
65  return value;
66  }
67 
71  [NonVersionable]
72  [__DynamicallyInvokable]
73  public T GetValueOrDefault(T defaultValue)
74  {
75  if (!hasValue)
76  {
77  return defaultValue;
78  }
79  return value;
80  }
81 
90  [__DynamicallyInvokable]
91  public override bool Equals(object other)
92  {
93  if (!hasValue)
94  {
95  return other == null;
96  }
97  if (other == null)
98  {
99  return false;
100  }
101  return value.Equals(other);
102  }
103 
106  [__DynamicallyInvokable]
107  public override int GetHashCode()
108  {
109  if (!hasValue)
110  {
111  return 0;
112  }
113  return value.GetHashCode();
114  }
115 
118  [__DynamicallyInvokable]
119  public override string ToString()
120  {
121  if (!hasValue)
122  {
123  return "";
124  }
125  return value.ToString();
126  }
127 
131  [NonVersionable]
132  [__DynamicallyInvokable]
133  public static implicit operator T?(T value)
134  {
135  return value;
136  }
137 
141  [NonVersionable]
142  [__DynamicallyInvokable]
143  public static explicit operator T(T? value)
144  {
145  return value.Value;
146  }
147  }
149  [ComVisible(true)]
150  [__DynamicallyInvokable]
151  public static class Nullable
152  {
158  [ComVisible(true)]
159  [__DynamicallyInvokable]
160  public static int Compare<T>(T? n1, T? n2) where T : struct
161  {
162  if (n1.HasValue)
163  {
164  if (n2.HasValue)
165  {
166  return Comparer<T>.Default.Compare(n1.value, n2.value);
167  }
168  return 1;
169  }
170  if (n2.HasValue)
171  {
172  return -1;
173  }
174  return 0;
175  }
176 
187  [ComVisible(true)]
188  [__DynamicallyInvokable]
189  public static bool Equals<T>(T? n1, T? n2) where T : struct
190  {
191  if (n1.HasValue)
192  {
193  if (n2.HasValue)
194  {
195  return EqualityComparer<T>.Default.Equals(n1.value, n2.value);
196  }
197  return false;
198  }
199  if (n2.HasValue)
200  {
201  return false;
202  }
203  return true;
204  }
205 
211  [__DynamicallyInvokable]
212  public static Type GetUnderlyingType(Type nullableType)
213  {
214  if ((object)nullableType == null)
215  {
216  throw new ArgumentNullException("nullableType");
217  }
218  Type result = null;
219  if (nullableType.IsGenericType && !nullableType.IsGenericTypeDefinition)
220  {
221  Type genericTypeDefinition = nullableType.GetGenericTypeDefinition();
222  if ((object)genericTypeDefinition == typeof(Nullable<>))
223  {
224  result = nullableType.GetGenericArguments()[0];
225  }
226  }
227  return result;
228  }
229  }
230 }
virtual Type GetGenericTypeDefinition()
Returns a T:System.Type object that represents a generic type definition from which the current gener...
Definition: Type.cs:2443
virtual bool IsGenericTypeDefinition
Gets a value indicating whether the current T:System.Type represents a generic type definition,...
Definition: Type.cs:579
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static bool Equals< T >(T? n1, T? n2)
Indicates whether two specified T:System.Nullable`1 objects are equal.
Definition: Nullable.cs:189
Provides a base class for implementations of the T:System.Collections.Generic.IEqualityComparer`1 gen...
Provides a base class for implementations of the T:System.Collections.Generic.IComparer`1 generic int...
Definition: Comparer.cs:11
override bool Equals(object other)
Indicates whether the current T:System.Nullable`1 object is equal to a specified object.
Definition: Nullable.cs:91
Definition: __Canon.cs:3
virtual bool IsGenericType
Gets a value indicating whether the current type is a generic type.
Definition: Type.cs:566
Represents a value type that can be assigned null.
Definition: Nullable.cs:12
static EqualityComparer< T > Default
Returns a default equality comparer for the type specified by the generic argument.
static Type GetUnderlyingType(Type nullableType)
Returns the underlying type argument of the specified nullable type.
Definition: Nullable.cs:212
bool HasValue
Gets a value indicating whether the current T:System.Nullable`1 object has a valid value of its under...
Definition: Nullable.cs:23
virtual Type [] GetGenericArguments()
Returns an array of T:System.Type objects that represent the type arguments of a closed generic type ...
Definition: Type.cs:2433
static Comparer< T > Default
Returns a default sort order comparer for the type specified by the generic argument.
Definition: Comparer.cs:19
static int Compare< T >(T? n1, T? n2)
Compares the relative values of two T:System.Nullable`1 objects.
Definition: Nullable.cs:160
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Nullable(T value)
Initializes a new instance of the T:System.Nullable`1 structure to the specified value.
Definition: Nullable.cs:53
T GetValueOrDefault()
Retrieves the value of the current T:System.Nullable`1 object, or the object's default value.
Definition: Nullable.cs:63
Specifies that the class can be serialized.
T Value
Gets the value of the current T:System.Nullable`1 object if it has been assigned a valid underlying v...
Definition: Nullable.cs:37
override int GetHashCode()
Retrieves the hash code of the object returned by the P:System.Nullable`1.Value property.
Definition: Nullable.cs:107
override string ToString()
Returns the text representation of the value of the current T:System.Nullable`1 object.
Definition: Nullable.cs:119
T GetValueOrDefault(T defaultValue)
Retrieves the value of the current T:System.Nullable`1 object, or the specified default value.
Definition: Nullable.cs:73