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 methods159GUIDELINES FOR USING CAPTURED VARIABLESThe follow<strong>in</strong>g is a list of suggestions for us<strong>in</strong>g captured variables:■ If code that doesn’t use captured variables is just as simple as code that does,don’t use them.■ Before captur<strong>in</strong>g a variable declared by a for or foreach statement, considerwhether your delegate is go<strong>in</strong>g to live beyond the loop iteration, and whetheryou want it to see the subsequent values of that variable. If not, create anothervariable <strong>in</strong>side the loop that just copies the value you do want.■ If you create multiple delegate <strong>in</strong>stances (whether <strong>in</strong> a loop or explicitly) thatcapture variables, put thought <strong>in</strong>to whether you want them to capture the samevariable.■ If you capture a variable that doesn’t actually change (either <strong>in</strong> the anonymousmethod or the enclos<strong>in</strong>g method body), then you don’t need to worry as much.■ If the delegate <strong>in</strong>stances you create never “escape” from the method—<strong>in</strong> otherwords, they’re never stored anywhere else, or returned, or used for start<strong>in</strong>gthreads—life is a lot simpler.■ Consider the extended lifetime of any captured variables <strong>in</strong> terms of garbagecollection. This is normally not an issue, but if you capture an object that isexpensive <strong>in</strong> terms of memory, it may be significant.The first po<strong>in</strong>t is the golden rule. Simplicity is a good th<strong>in</strong>g—so any time the use of acaptured variable makes your code simpler (after you’ve factored <strong>in</strong> the additional<strong>in</strong>herent complexity of forc<strong>in</strong>g your code’s ma<strong>in</strong>ta<strong>in</strong>ers to understand what the capturedvariable does), use it. You need to <strong>in</strong>clude that extra complexity <strong>in</strong> your considerations,that’s all—don’t just go for m<strong>in</strong>imal l<strong>in</strong>e count.We’ve covered a lot of ground <strong>in</strong> this section, and I’m aware that it can be hard totake <strong>in</strong>. I’ve listed the most important th<strong>in</strong>gs to remember next, so that if you need tocome back to this section another time you can jog your memory without hav<strong>in</strong>g toread through the whole th<strong>in</strong>g aga<strong>in</strong>:■ The variable is captured—not its value at the po<strong>in</strong>t of delegate <strong>in</strong>stance creation.■ Captured variables have lifetimes extended to at least that of the captur<strong>in</strong>g delegate.■ Multiple delegates can capture the same variable…■ …but with<strong>in</strong> loops, the same variable declaration can effectively refer to differentvariable “<strong>in</strong>stances.”■ for/foreach loop declarations create variables that live for the duration of theloop—they’re not <strong>in</strong>stantiated on each iteration.■ Captured variables aren’t really local variables—extra types are created wherenecessary.■ Be careful! Simple is almost always better than clever.We’ll see more variables be<strong>in</strong>g captured when we look at <strong>C#</strong> 3 and its lambda expressions,but for now you may be relieved to hear that we’ve f<strong>in</strong>ished our rundown of thenew <strong>C#</strong> 2 delegate features.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!