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.

<strong>C#</strong> 2’s syntactic sugar for nullable types121doesn’t mention it, and the documentation for Nullable itself doesn’t mention it.I’ve honored that difference by wait<strong>in</strong>g until we’re specifically talk<strong>in</strong>g about <strong>C#</strong> 2itself before <strong>in</strong>troduc<strong>in</strong>g the term.With that out of the way, let’s see what features <strong>C#</strong> 2 gives us, start<strong>in</strong>g by reduc<strong>in</strong>gthe clutter <strong>in</strong> our code.4.3.1 The ? modifierThere are some elements of syntax that may be unfamiliar at first but have an appropriatefeel to them. The conditional operator (a ? b : c) is one of them for me—it asksa question and then has two correspond<strong>in</strong>g answers. In the same way, the ? operatorfor nullable types just feels right to me.It’s a shorthand way of us<strong>in</strong>g a nullable type, so <strong>in</strong>stead of us<strong>in</strong>g Nullable we can use byte? throughout our code. The two are <strong>in</strong>terchangeable and compile toexactly the same IL, so you can mix and match them if you want to, but on behalf ofwhoever reads your code next, I’d urge you to pick one way or the other and use itconsistently. List<strong>in</strong>g 4.3 is exactly equivalent to list<strong>in</strong>g 4.2 but uses the ? modifier.List<strong>in</strong>g 4.3The same code as list<strong>in</strong>g 4.2 but us<strong>in</strong>g the ? modifier<strong>in</strong>t? nullable = 5;object boxed = nullable;Console.WriteL<strong>in</strong>e(boxed.GetType());<strong>in</strong>t normal = (<strong>in</strong>t)boxed;Console.WriteL<strong>in</strong>e(normal);nullable = (<strong>in</strong>t?)boxed;Console.WriteL<strong>in</strong>e(nullable);nullable = new <strong>in</strong>t?();boxed = nullable;Console.WriteL<strong>in</strong>e (boxed==null);nullable = (<strong>in</strong>t?)boxed;Console.WriteL<strong>in</strong>e(nullable.HasValue);I won’t go through what the code does or how it does it, because the result is exactly thesame as list<strong>in</strong>g 4.2. The two list<strong>in</strong>gs compile down to the same IL—they’re just us<strong>in</strong>g differentsyntax, just as us<strong>in</strong>g <strong>in</strong>t is <strong>in</strong>terchangeable with System.Int32. The only changesare the ones <strong>in</strong> bold. You can use the shorthand version everywhere, <strong>in</strong>clud<strong>in</strong>g <strong>in</strong>method signatures, typeof expressions, casts, and the like.The reason I feel the modifier is very well chosen is that it adds an air of uncerta<strong>in</strong>tyto the nature of the variable. Does the variable nullable <strong>in</strong> list<strong>in</strong>g 4.3 have an<strong>in</strong>teger value? Well, at any particular time it might, or it might be the null value. Fromnow on, we’ll use the ? modifier <strong>in</strong> all the examples—it’s neater, and it’s arguably theidiomatic way to use nullable types <strong>in</strong> <strong>C#</strong> 2. However, you may feel that it’s too easy tomiss when read<strong>in</strong>g the code, <strong>in</strong> which case there’s certa<strong>in</strong>ly noth<strong>in</strong>g to stop you fromus<strong>in</strong>g the longer syntax. You may wish to compare the list<strong>in</strong>gs <strong>in</strong> this section and theprevious one to see which you f<strong>in</strong>d clearer.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!