13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

150 CHAPTER 5 Fast-tracked delegatesto call different method overloads) then the compiler needs more help. To show youwhat I mean, let’s look at how we start threads. There are four thread constructors <strong>in</strong>.NET 2.0:public Thread (ParameterizedThreadStart start)public Thread (ThreadStart start)public Thread (ParameterizedThreadStart start, <strong>in</strong>t maxStackSize)public Thread (ThreadStart start, <strong>in</strong>t maxStackSize)The two delegate types <strong>in</strong>volved arepublic delegate void ThreadStart()public delegate void ParameterizedThreadStart(object obj)Now, consider the follow<strong>in</strong>g three attempts to create a new thread:new Thread(delegate() { Console.WriteL<strong>in</strong>e("t1"); } );new Thread(delegate(object o) { Console.WriteL<strong>in</strong>e("t2"); } );new Thread(delegate { Console.WriteL<strong>in</strong>e("t3"); } );The first and second l<strong>in</strong>es conta<strong>in</strong> parameter lists—the compiler knows that it can’t convertthe anonymous method <strong>in</strong> the first l<strong>in</strong>e <strong>in</strong>to a ParameterizedThreadStart, or convertthe anonymous method <strong>in</strong> the second l<strong>in</strong>e <strong>in</strong>to a ThreadStart. Those l<strong>in</strong>es compile,because there’s only one applicable constructor overload <strong>in</strong> each case. The third l<strong>in</strong>e,however, is ambiguous—the anonymous method can be converted <strong>in</strong>to either delegatetype, so both of the constructor overloads tak<strong>in</strong>g just one parameter are applicable. Inthis situation, the compiler throws its hands up and issues an error. You can solve thiseither by specify<strong>in</strong>g the parameter list explicitly or cast<strong>in</strong>g the anonymous method to theright delegate type.Hopefully what you’ve seen of anonymous methods so far will have provokedsome thought about your own code, and made you consider where you could usethese techniques to good effect. Indeed, even if anonymous methods could only dowhat we’ve already seen, they’d still be very useful. However, there’s more to anonymousmethods than just avoid<strong>in</strong>g the <strong>in</strong>clusion of an extra method <strong>in</strong> your code.Anonymous methods are <strong>C#</strong> 2’s implementation of a feature known elsewhere as closuresby way of captured variables. Our next section expla<strong>in</strong>s both of these terms andshows how anonymous methods can be extremely powerful—and confus<strong>in</strong>g if you’renot careful.5.5 Captur<strong>in</strong>g variables <strong>in</strong> anonymous methodsI don’t like hav<strong>in</strong>g to give warn<strong>in</strong>gs, but I th<strong>in</strong>k it makes sense to <strong>in</strong>clude one here: ifthis topic is new to you, then don’t start this section until you’re feel<strong>in</strong>g reasonablyawake and have a bit of time to spend on it. I don’t want to alarm you unnecessarily,and you should feel confident that there’s noth<strong>in</strong>g so <strong>in</strong>sanely complicated that youwon’t be able to understand it with a little effort. It’s just that captured variables canbe somewhat confus<strong>in</strong>g to start with, partly because they overturn some of your exist<strong>in</strong>gknowledge and <strong>in</strong>tuition.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!