13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

System.Nullable and System.Nullable117ToStr<strong>in</strong>g on the value if there is. Equals is slightly more complicated—we’ll comeback to it when we’ve discussed box<strong>in</strong>g.F<strong>in</strong>ally, two conversions are provided by the framework. First, there is an implicitconversion from T to Nullable. This always results <strong>in</strong> an <strong>in</strong>stance where HasValuereturns true. Likewise, there is an explicit operator convert<strong>in</strong>g from Nullable toT, which behaves exactly the same as the Value property, <strong>in</strong>clud<strong>in</strong>g throw<strong>in</strong>g an exceptionwhen there is no real value to return.NOTEWrapp<strong>in</strong>g and unwrapp<strong>in</strong>g—The <strong>C#</strong> specification names the process ofconvert<strong>in</strong>g an <strong>in</strong>stance of T to an <strong>in</strong>stance of Nullable wrapp<strong>in</strong>g, withthe obvious opposite process be<strong>in</strong>g called unwrapp<strong>in</strong>g. The <strong>C#</strong> specificationactually def<strong>in</strong>es these terms with reference to the constructor tak<strong>in</strong>ga parameter and the Value property, respectively. Indeed these calls aregenerated by the <strong>C#</strong> code, even when it otherwise looks as if you’re us<strong>in</strong>gthe conversions provided by the framework. The results are the sameeither way, however. For the rest of this chapter, I won’t dist<strong>in</strong>guishbetween the two implementations available.Before we go any further, let’s see all this <strong>in</strong> action. List<strong>in</strong>g 4.1 shows everyth<strong>in</strong>g youcan do with Nullable directly, leav<strong>in</strong>g Equals aside for the moment.List<strong>in</strong>g 4.1Us<strong>in</strong>g various members of Nullablestatic void Display(Nullable x){Console.WriteL<strong>in</strong>e ("HasValue: {0}", x.HasValue);if (x.HasValue){Console.WriteL<strong>in</strong>e ("Value: {0}", x.Value);Console.WriteL<strong>in</strong>e ("Explicit conversion: {0}", (<strong>in</strong>t)x);}Console.WriteL<strong>in</strong>e ("GetValueOrDefault(): {0}",x.GetValueOrDefault());Console.WriteL<strong>in</strong>e ("GetValueOrDefault(10): {0}",x.GetValueOrDefault(10));Console.WriteL<strong>in</strong>e ("ToStr<strong>in</strong>g(): \"{0}\"", x.ToStr<strong>in</strong>g());Console.WriteL<strong>in</strong>e ("GetHashCode(): {0}", x.GetHashCode());Console.WriteL<strong>in</strong>e ();}...Nullable x = 5;x = new Nullable(5);Console.WriteL<strong>in</strong>e("Instance with value:");Display(x);x = new Nullable();Console.WriteL<strong>in</strong>e("Instance without value:");Display(x);In list<strong>in</strong>g 4.1 we first show the two different ways (<strong>in</strong> terms of <strong>C#</strong> source code) of wrapp<strong>in</strong>ga value of the underly<strong>in</strong>g type, and then we use various different members on theLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!