PETR
programming c# 4.0.pdf

We set up a local variable called as we move through

Info icon This preview shows pages 192–195. Sign up to view the full content.

we set up a local variable called documentBeingProcessed . As we move through the documents we update that variable to reflect our current status. How do we get that information into the lambda expression? Simple: we just use it! 168 | Chapter 5: Composability and Extensibility with Delegates
Image of page 192

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Compile and run that code, and you’ll see the following output: The processing will not succeed. (Document 1) Document traduit. (Document 2) Spellchecked document. (Document 2) Repaginated document. (Document 2) Highlighting 'millennium' (Document 2) Document traduit. (Document 3) Spellchecked document. (Document 3) Repaginated document. (Document 3) We took advantage of the fact that an anonymous method has access to variables declared in its parent scope , in addition to anything in its own scope. For more information about this, see the sidebar below. Closures In general, we call an instance of a function and the set of variables on which it operates a closure . In a pure functional language, a closure is typically implemented by taking a snapshot of the values of the variables at the time at which the closure is created, along with a reference to the function concerned, and those values are immutable. In C#, a similar technique is applied—but the language allows us to modify those variables after the closure has been created . As we see in this chapter, we can use this to our advantage, but we have to be careful to understand and manage the scope of the variables in the closure to avoid peculiar side effects. We’ve seen how to read variables in our containing scope, but what about writing back to them? That works too. Let’s create a process counter that ticks up every time we execute a process, and add it to our logging function (see Example 5-20 ). Example 5-20. Modifying surrounding variables from a nested method static void Main(string[] args) { // ... (document setup) DocumentProcessor processor = Configure(); Generic Delegates for Functions | 169
Image of page 193
string documentBeingProcessed = "(No document set)"; int processCount = 0; processor.LogTextProvider = (doc => { processCount += 1; return documentBeingProcessed; }); documentBeingProcessed = "(Document 1)"; processor.Process(doc1); Console.WriteLine(); documentBeingProcessed = "(Document 2)"; processor.Process(doc2); Console.WriteLine(); documentBeingProcessed = "(Document 3)"; processor.Process(doc3); Console.WriteLine(); Console.WriteLine("Executed " + processCount + " processes."); Console.ReadKey(); } We added a processCount variable at method scope, which we initialized to zero. We’ve switched our lambda expression into the statement form with the braces so that we can write multiple statements in the function body. In addition to returning the name of the document being processed, we also increment our processCount . Finally, at the end of processing, we write out a line that tells us how many processes we’ve executed. So our output looks like this: The processing will not succeed.
Image of page 194

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 195
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern