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.

56 CHAPTER 2 Core foundations: build<strong>in</strong>g on <strong>C#</strong> 1a lot more sense. In <strong>C#</strong> 1, it didn’t matter whether or not two different event handlerslooked “quite similar”—you had to have a method with an exactly match<strong>in</strong>g signature<strong>in</strong> order to create a delegate <strong>in</strong>stance. In <strong>C#</strong> 2, you may well f<strong>in</strong>d yourself able to usethe same method to handle many different k<strong>in</strong>ds of events, particularly if the purposeof the method is fairly event <strong>in</strong>dependent, such as logg<strong>in</strong>g.<strong>C#</strong> 3 provides special syntax for <strong>in</strong>stantiat<strong>in</strong>g delegate types, us<strong>in</strong>g lambda expressions.To demonstrate these, we’ll use a new delegate type. As part of the CLR ga<strong>in</strong><strong>in</strong>ggenerics <strong>in</strong> .NET 2.0, generic delegate types became available and were used <strong>in</strong> a numberof API calls <strong>in</strong> generic collections. However, .NET 3.5 takes th<strong>in</strong>gs a step further,<strong>in</strong>troduc<strong>in</strong>g a group of generic delegate types called Func that all take a number ofparameters of specified types and return a value of another specified type. List<strong>in</strong>g 2.5gives an example of the use of a Func delegate type as well as lambda expressions.List<strong>in</strong>g 2.5Lambda expressions, which are like improved anonymous methodsFunc func = (x,y) => (x*y).ToStr<strong>in</strong>g();Console.WriteL<strong>in</strong>e (func(5, 20));Func is a delegate type that takes two <strong>in</strong>tegers and returns a str<strong>in</strong>g.The lambda expression <strong>in</strong> list<strong>in</strong>g 2.5 specifies that the delegate <strong>in</strong>stance (held <strong>in</strong>func) should multiply the two <strong>in</strong>tegers together and call ToStr<strong>in</strong>g(). The syntax ismuch more straightforward than that of anonymous methods, and there are otherbenefits <strong>in</strong> terms of the amount of type <strong>in</strong>ference the compiler is prepared to performfor you. Lambda expressions are absolutely crucial to LINQ, and you should get readyto make them a core part of your language toolkit. They’re not restricted to work<strong>in</strong>gwith LINQ, however—almost any use of anonymous methods from <strong>C#</strong> 2 can uselambda expressions <strong>in</strong> <strong>C#</strong> 3.To summarize, the new features related to delegates are as follows:■ Generics (generic delegate types)—<strong>C#</strong> 2■ Delegate <strong>in</strong>stance creation expressions—<strong>C#</strong> 2■ Anonymous methods—<strong>C#</strong> 2■ Delegate covariance/contravariance—<strong>C#</strong> 2■ Lambda expressions—<strong>C#</strong> 3The use of generics extends well beyond delegates, of course—they’re one of the pr<strong>in</strong>cipleenhancements to the type system, which we’ll look at next.2.4.2 Features related to the type systemThe primary new feature <strong>in</strong> <strong>C#</strong> 2 regard<strong>in</strong>g the type system is that of generics. Itlargely addresses the issues I raised <strong>in</strong> section 2.2.2 about strongly typed collections,although generic types are useful <strong>in</strong> a number of other situations too. As a feature, it’selegant, it solves a real problem, and despite a few wr<strong>in</strong>kles it generally works verywell. We’ve seen examples of this <strong>in</strong> quite a few places already, and it’s described fully<strong>in</strong> the next chapter, so I won’t go <strong>in</strong>to any more details here. It’ll be a brief reprieve,Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!