Chapter 16. C# 5 bonus features and closing thoughts

 

This chapter covers

  • Changes to captured variables
  • Caller information attributes
  • Closing thoughts

C# 2 had a bunch of small but disparate features, along with the major ones. C# 3 had several minor features building up to LINQ. Even C# 4 had relatively small features worth going into some detail about.

C# 5 has almost no features beyond asynchrony. It has just two little extras, both tiny. The C# design team always weighs the cost of a feature (in terms of design, implementation, testing, documentation, and developer education) against its benefits. I’m sure there are plenty of outstanding feature requests the team would like to satisfy, so presumably the costs of these bite-sized features were just small enough to allow them to make the cut.

The first change isn’t so much a feature as a correction to an earlier mistake in the language design...

16.1. Changes to captured variables in foreach loops

Back in section 5.5.5 I gave a warning about code that used an anonymous function (typically a lambda expression) within a foreach loop, capturing the loop variable. The following listing shows a simple example of such code, which looks as if it will output x, then y, and then z.

Listing 16.1. Using captured iteration variables

16.2. Caller information attributes

 
 

16.3. Closing thoughts

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest