(You are currently looking at the first edition version of this page. This page is also available for the second and third editions.)

Errata for Chapter 5: Fast-tracked delegates

P141, start of 5.3: Incorrect cross reference

At the start of section 5.3, I refer back to section 2.3.2 for discussion of covariance and contravariance. This should in fact be a reference to 2.2.2.

P155, first paragraph: Variable "i" should be "counter"

The second sentence on page 155 currently reads:

The first line of output comes from the invocation of the delegate instance within CreateDelegateInstance, so it makes sense that the value of i is available at that point.

Unfortunately, there is no variable i - there's counter instead! The sentence should read:

The first line of output comes from the invocation of the delegate instance within CreateDelegateInstance, so it makes sense that the value of counter is available at that point.

P157, first paragraph: Loop only displays up to 13, not 14

On page 157, in section 5.5.5, I claim that if we'd captured the index variable instead of counter, the output would have been the numbers 5 to 14. This isn't the case - it would actually be 5 to 13. Here's the code, ready to run in Snippy:

List<ThreadStart> list = new List<ThreadStart>();

for (int index=0; index < 5; index++)
{
   // counter variable is now obsolete
   int counter = index*10;
   list.Add (delegate
      {
         // index variable is captured
         Console.WriteLine(index);
         index++;
      }
   );
}

foreach (ThreadStart t in list)
{
   t();
}

list[0]();
list[0]();
list[0]();
list[1]();

And here's the output...

5
6
7
8
9
10
11
12
13

Thanks to Dave Jordan for pointing this out.