Prev | Current Page 274 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

(The primary change in C# 3 supporting delegates is
in lambda expressions, which we??™ll meet in chapter 9.)
This chapter covers
?–  Longwinded C# 1 syntax
?–  Simplified delegate construction
?–  Covariance and contravariance
?–  Anonymous methods
?–  Captured variables
138 CHAPTER 5 Fast-tracked delegates
C# 2 is a sort of stepping stone in terms of delegates. Its new features pave the way
for the even more dramatic changes of C# 3, keeping developers reasonably comfortable
while still providing useful benefits. The extent to which this was a finely balanced
act as opposed to intuition and a following wind is likely to stay unknown, but
we can certainly reap the benefits.
Delegates play a more prominent part in .NET 2.0 than in earlier versions,
although they??™re not as common as they are in .NET 3.5. In chapter 3 we saw how they
can be used to convert from a list of one type to a list of another type, and way back in
chapter 1 we sorted a list of products using the Comparison delegate instead of the
IComparer interface. Although the framework and C# keep a respectful distance from
each other where possible, I believe that the language and platform drove each other
here: the inclusion of more delegate-based API calls supported the improved syntax
available in C# 2, and vice versa.
In this chapter we??™ll see how C# 2 makes two small changes that make life easier
when creating delegate instances from normal methods, and then we??™ll look at the
biggest change: anonymous methods, which allow you to specify a delegate instance??™s
action inline at the point of its creation.


Pages:
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286