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.

58 CHAPTER 2 Core foundations: build<strong>in</strong>g on <strong>C#</strong> 1Extension methods also let you appear to add methods with implementations to<strong>in</strong>terfaces—and <strong>in</strong>deed that’s what LINQ relies on heavily, allow<strong>in</strong>g calls to all k<strong>in</strong>ds ofmethods on IEnumerable that have never previously existed.Here’s the quick-view list of these features, along with which version of <strong>C#</strong> they’re<strong>in</strong>troduced <strong>in</strong>:■ Generics—<strong>C#</strong> 2■ Limited delegate covariance/contravariance—<strong>C#</strong> 2■ Anonymous types—<strong>C#</strong> 3■ Implicit typ<strong>in</strong>g—<strong>C#</strong> 3■ Extension methods—<strong>C#</strong> 3After that fairly diverse set of features on the type system <strong>in</strong> general, let’s look at thefeatures added to one very specific part of typ<strong>in</strong>g <strong>in</strong> .NET—value types.2.4.3 Features related to value typesThere are only two features to talk about here, and <strong>C#</strong> 2 <strong>in</strong>troduces them both. Thefirst goes back to generics yet aga<strong>in</strong>, and <strong>in</strong> particular collections. One common compla<strong>in</strong>tabout us<strong>in</strong>g value types <strong>in</strong> collections with .NET 1.1 was that due to all of the“general purpose” APIs be<strong>in</strong>g specified <strong>in</strong> terms of the object type, every operationthat added a struct value to a collection would <strong>in</strong>volve box<strong>in</strong>g it, and when retriev<strong>in</strong>g ityou’d have to unbox it. While box<strong>in</strong>g is pretty cheap on a “per call” basis, it can causea significant performance hit when it’s used every time with frequently accessed collections.It also takes more memory than it needs to, due to the per-object overhead.Generics fix both the speed and memory deficiencies by us<strong>in</strong>g the real type <strong>in</strong>volvedrather than just a general-purpose object. As an example, it would have been madnessto read a file and store each byte as an element <strong>in</strong> an ArrayList <strong>in</strong> .NET 1.1—but <strong>in</strong>.NET 2.0 it wouldn’t be particularly crazy to do the same with a List.The second feature addresses another common cause of compla<strong>in</strong>t, particularlywhen talk<strong>in</strong>g to databases—the fact that you can’t assign null to a value type variable.There’s no such concept as an <strong>in</strong>t value of null, for <strong>in</strong>stance, even though a database<strong>in</strong>teger field may well be nullable. At that po<strong>in</strong>t it can be hard to model the databasetable with<strong>in</strong> a statically typed class without a bit of ugl<strong>in</strong>ess of some form or another.Nullable types are part of .NET 2.0, and <strong>C#</strong> 2 <strong>in</strong>cludes extra syntax to make them easyto use. List<strong>in</strong>g 2.7 gives a brief example of this.List<strong>in</strong>g 2.7Demonstration of a variety of nullable type features<strong>in</strong>t? x = null; Declares and sets nullable variablex = 5;if (x != null) Tests for presence of “real” value{<strong>in</strong>t y = x.Value; Obta<strong>in</strong>s “real” valueConsole.WriteL<strong>in</strong>e (y);}<strong>in</strong>t z = x ?? 10; Uses null-coalesc<strong>in</strong>g operatorLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!