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.

Part 2<strong>C#</strong>2:solv<strong>in</strong>g the issuesof <strong>C#</strong>1In part 1 we took a quick look at a few of the features of <strong>C#</strong> 2. Now it’s time todo the job properly. We’ll see how <strong>C#</strong> 2 fixes various problems that developersran <strong>in</strong>to when us<strong>in</strong>g <strong>C#</strong> 1, and how <strong>C#</strong> 2 makes exist<strong>in</strong>g features more useful bystreaml<strong>in</strong><strong>in</strong>g them. This is no mean feat, and life with <strong>C#</strong> 2 is much more pleasantthan with <strong>C#</strong> 1.The new features <strong>in</strong> <strong>C#</strong> 2 have a certa<strong>in</strong> amount of <strong>in</strong>dependence. That’s notto say they’re not related at all, of course; many of the features are based on—orat least <strong>in</strong>teract with—the massive contribution that generics make to the language.However, the different topics we’ll look at <strong>in</strong> the next five chapters don’tcomb<strong>in</strong>e <strong>in</strong>to one cohesive whole.The first four chapters of this part cover the biggest new features. We’ll lookat the follow<strong>in</strong>g:■ Generics-—The most important new feature <strong>in</strong> <strong>C#</strong> 2 (and <strong>in</strong>deed <strong>in</strong> theCLR for .NET 2.0), generics allow type and method parameterization.■ Nullable types —Value types such as <strong>in</strong>t and DateTime don’t have any conceptof “no value present”—nullable types allow you to represent theabsence of a mean<strong>in</strong>gful value.■ Delegates —Although delegates haven’t changed at the CLR level, <strong>C#</strong> 2makes them a lot easier to work with. As well as a few simple shortcuts, the<strong>in</strong>troduction of anonymous methods beg<strong>in</strong>s the movement toward a morefunctional style of programm<strong>in</strong>g—this trend cont<strong>in</strong>ues <strong>in</strong> <strong>C#</strong> 3.■ Iterators —While us<strong>in</strong>g iterators has always been simple <strong>in</strong> <strong>C#</strong> with theforeach statement, it’s a pa<strong>in</strong> to implement them <strong>in</strong> <strong>C#</strong> 1. The <strong>C#</strong> 2 compileris happy to build a state mach<strong>in</strong>e for you beh<strong>in</strong>d the scenes, hid<strong>in</strong>g alot of the complexity <strong>in</strong>volved.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!