Wednesday, October 24, 2007

new in .NET Anonymous methods

  • Anonymous Methods (C# Programming Guide)
    In versions of C# previous to 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduces anonymous methods.
    Creating anonymous methods is essentially a way to pass a code block as a delegate parameter.
    By using anonymous methods, you reduce the coding overhead in instantiating delegates by eliminating the need to create a separate method.
    C#
    Copy Code
    // Create a delegate instance
    delegate void Del(int x);

    // Instantiate the delegate using an anonymous method
    Del d = delegate(int k) { /* ... */ };

    Ø The scope of the parameters of an anonymous method is the anonymous-method-block.

    Ø It is an error to have a jump statement, such as goto, break, or continue, inside the anonymous method block whose target is outside the block. It is also an error to have a jump statement, such as goto, break, or continue, outside the anonymous method block whose target is inside the block.

    Ø The local variables and parameters whose scope contain an anonymous method declaration are called outer or captured variables of the anonymous method.

    Ø For example, in the following code segment, n is an outer variable:
    C#
    Copy Code
    int n = 0; Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };
    Unlike local variables, the lifetime of the outer variable extends until the delegates that reference the anonymous methods are eligible for garbage collection. A reference to n is captured at the time the delegate is created.
    Ø An anonymous method cannot access the ref or out parameters of an outer scope.
    Ø No unsafe code can be accessed within the anonymous-method-block.
    Example
    The following example demonstrates the two ways of instantiating a delegate:
    Associating the delegate with an anonymous method.
    Associating the delegate with a named method (DoWork).
    In each case, a message is displayed when the delegate is invoked.
    C#
    Copy Code
    // Declare a delegate
    delegate void Printer(string s);
    class TestClass { static void Main() {
    // Instatiate the delegate type using an anonymous method:
    Printer p = delegate(string j)
    {
    System.Console.WriteLine(j);
    };
    // Results from the anonymous delegate call:
    p("The delegate using the anonymous method is called.");
    // The delegate instantiation using a named method "DoWork":
    p = new Printer(TestClass.DoWork);
    // Results from the old style delegate call:
    p("The delegate using the named method is called.");
    }
    // The method associated with the named delegate:
    static void DoWork(string k)
    {
    System.Console.WriteLine(k);
    }
    }
    var ExpCollDivStr = ExpCollDivStr;
    ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl22ba5ee29,";
    var ExpCollImgStr = ExpCollImgStr;
    ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl22img,";
    Output
    The delegate using the anonymous method is called.
    The delegate using the named method is called.

No comments: