13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

System.Nullable and System.Nullable119nullable = new Nullable();boxed = nullable;Console.WriteL<strong>in</strong>e (boxed==null);nullable = (Nullable)boxed;Console.WriteL<strong>in</strong>e(nullable.HasValue);Boxes a nullablewithout valueUnboxes tonullable variableThe output of list<strong>in</strong>g 4.2 shows that the type of the boxed value is pr<strong>in</strong>ted as System.Int32 (not System.Nullable). It then confirms that we can retrievethe value by unbox<strong>in</strong>g to either just <strong>in</strong>t or to Nullable. F<strong>in</strong>ally, the output demonstrateswe can box from a nullable <strong>in</strong>stance without a value to a null reference andsuccessfully unbox aga<strong>in</strong> to another value-less nullable <strong>in</strong>stance. If we’d tried unbox<strong>in</strong>gthe last value of boxed to a non-nullable <strong>in</strong>t, the program would have blown up with aNullReferenceException.Now that we understand the behavior of box<strong>in</strong>g and unbox<strong>in</strong>g, we can beg<strong>in</strong> totackle the behavior of Nullable.Equals.4.2.3 Equality of Nullable <strong>in</strong>stancesNullable overrides object.Equals(object) but doesn’t <strong>in</strong>troduce any equalityoperators or provide an Equals(Nullable) method. S<strong>in</strong>ce the framework has suppliedthe basic build<strong>in</strong>g blocks, languages can add extra functionality on top, <strong>in</strong>clud<strong>in</strong>gmak<strong>in</strong>g exist<strong>in</strong>g operators work as we’d expect them to. We’ll see the details ofthat <strong>in</strong> section 4.3.3, but the basic equality as def<strong>in</strong>ed by the vanilla Equals methodfollows these rules for a call to first.Equals(second):■ If first has no value and second is null, they are equal.■ If first has no value and second isn’t null, they aren’t equal.■ If first has a value and second is null, they aren’t equal.■ Otherwise, they’re equal if first’s value is equal to second.Note that we don’t have to consider the case where second is another Nullablebecause the rules of box<strong>in</strong>g prohibit that situation. The type of second is object, so <strong>in</strong>order to be a Nullable it would have to be boxed, and as we have just seen, box<strong>in</strong>ga nullable <strong>in</strong>stance creates a box of the non-nullable type or returns a null reference.The rules are consistent with the rules of equality elsewhere <strong>in</strong> .NET, so you can usenullable <strong>in</strong>stances as keys for dictionaries and any other situations where you needequality. Just don’t expect it to differentiate between a non-nullable <strong>in</strong>stance and anullable <strong>in</strong>stance with a value—it’s all been carefully set up so that those two cases aretreated the same way as each other.That covers the Nullable structure itself, but it has a shadowy partner: theNullable class.4.2.4 Support from the nongeneric Nullable classThe System.Nullable struct does almost everyth<strong>in</strong>g you want it to. However, itreceives a little help from the System.Nullable class. This is a static class—it onlyLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!