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.

Captur<strong>in</strong>g variables <strong>in</strong> anonymous methods151Stick with it, though! The payback can be massive <strong>in</strong> terms of code simplicity andreadability. This topic will also be crucial when we come to look at lambda expressionsand LINQ <strong>in</strong> <strong>C#</strong> 3, so it’s worth the <strong>in</strong>vestment. Let’s start off with a few def<strong>in</strong>itions.5.5.1 Def<strong>in</strong><strong>in</strong>g closures and different types of variablesThe concept of closures is a very old one, first implemented <strong>in</strong> Scheme, but it’s been ga<strong>in</strong><strong>in</strong>gmore prom<strong>in</strong>ence <strong>in</strong> recent years as more ma<strong>in</strong>stream languages have taken it onboard. The basic idea is that a function 5 is able to <strong>in</strong>teract with an environment beyondthe parameters provided to it. That’s all there is to it <strong>in</strong> abstract terms, but to understandhow it applies to <strong>C#</strong> 2, we need a couple more terms:■■An outer variable is a local variable or parameter 6 whose scope <strong>in</strong>cludes an anonymousmethod. The this reference also counts as an outer variable of anyanonymous method where it can be used.A captured outer variable (usually shortened to just “captured variable”) is an outervariable that is used with<strong>in</strong> an anonymous method. So to go back to closures,the function part is the anonymous method, and the environment it can <strong>in</strong>teractwith is the set of variables captured by it.That’s all very dry and may be hard to imag<strong>in</strong>e, but the ma<strong>in</strong> thrust is that an anonymousmethod can use local variables def<strong>in</strong>ed <strong>in</strong> the same method that declares it. Thismay not sound like a big deal, but <strong>in</strong> many situations it’s enormously handy—you canuse contextual <strong>in</strong>formation that you have “on hand” rather than hav<strong>in</strong>g to set up extratypes just to store data you already know. We’ll see some useful concrete examplessoon, I promise—but first it’s worth look<strong>in</strong>g at some code to clarify these def<strong>in</strong>itions.List<strong>in</strong>g 5.10 provides an example with a number of local variables. It’s just a s<strong>in</strong>glemethod, so it can’t be run on its own. I’m not go<strong>in</strong>g to expla<strong>in</strong> how it would work orwhat it would do yet, but just expla<strong>in</strong> how the different variables are classified.List<strong>in</strong>g 5.10Examples of different k<strong>in</strong>ds of variables with respect to anonymous methodsvoid Enclos<strong>in</strong>gMethod(){<strong>in</strong>t outerVariable = 5;str<strong>in</strong>g capturedVariable = "captured";BOuter variable(uncaptured)COuter variablecaptured byanonymousmethodif (DateTime.Now.Hour==23){<strong>in</strong>t normalLocalVariable = DateTime.Now.M<strong>in</strong>ute;Console.WriteL<strong>in</strong>e(normalLocalVariable);}ThreadStart x = delegate(){str<strong>in</strong>g anonLocal="local to anonymous method";EDLocal variable ofnormal methodLocal variableof anonymousmethod5This is general computer science term<strong>in</strong>ology, not <strong>C#</strong> term<strong>in</strong>ology.6Exclud<strong>in</strong>g ref and out parameters.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!