The rules are consistent with the rules of equality elsewhere in .NET, so you can use
nullable instances as keys for dictionaries and any other situations where you need
equality. Just don??™t expect it to differentiate between a non-nullable instance and a
nullable instance with a value??”it??™s all been carefully set up so that those two cases are
treated the same way as each other.
That covers the Nullable
structure itself, but it has a shadowy partner: the
Nullable class.
4.2.4 Support from the nongeneric Nullable class
The System.Nullable struct does almost everything you want it to. However, it
receives a little help from the System.Nullable class. This is a static class??”it only
Boxes a nullable
without value
Unboxes to
nullable variable
120 CHAPTER 4 Saying nothing with nullable types
contains static methods, and you can??™t create an instance of it.2 In fact, everything it
does could have been done equally well by other types, and if Microsoft had seen
where they were going right from the beginning, it might not have even existed??”
which would have saved a little confusion over what the two types are there for, aside
from anything else. However, this accident of history has three methods to its name,
and they??™re still useful.
The first two are comparison methods:
public static int Compare(Nullable n1, Nullable n2)
public static bool Equals(Nullable n1, Nullable n2)
Compare uses Comparer.
Pages:
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255