table of Contents

Introduction Parameter range of anonymous method Delegated example


Prior to c#2.0,We create the only form of delegation-the naming method.

And c#2.0-the introduction of anonymous methods,In versions ≥ c#3.0,We will replace anonymous methods with lambda expressions,And using lambda expressions as the preferred way to write inline code,Because it's more concise.

The anonymous method is,As the name suggests,Anonymous methods are methods without a name.The most obvious benefit of anonymous methods is:it can reduce the workload of writing another method;Another benefit is that you can directly access the caller's variables,Thereby reducing the complexity of passing parameters.

Anonymous method,It is not a predefined method,Instead, use a delegated code block,In use,It is not different from the ordinary method,But anonymous methods can reduce system overhead to a certain extent.

Anonymous method,Keywords:delegate.

Anonymous method,Use scenario:Usually, ① a temporary method is needed,And this method is used very few times;② The code of this method is very short.not long.

[Note] There is a situation,Anonymous methods which provide features not available in lambda expressions. That is, you can use an anonymous method to ignore the parameter list. This means that anonymous methods can be transformed into delegates with various signatures,And for lambda expressions,It's almost impossible.

To pass a code block as a delegate parameter,Using anonymous methods is currently the only way.

Example one:

 //create a click event
  button1.click +=delegate (object o, eventargs e)
       {messagebox.show ("click!");};

Example two:

//Create a delegate.
 delegate void mydel (int x);
 //use anonymous method and instantiate delegate
 mydel del=delegate (int k) {/* ... * /};

Using anonymous methods,Since there is no need to create a separate method,This reduces the coding overhead required by the two example instantiated delegates.

For example, if the overhead required to create a method is not necessary,It is useful to specify code blocks instead of delegates.

I am going through an example here,How to create and start a new thread as an anonymous method:

//New thread start example
Void start ()
  thread thread=new thread
   (delegate ()
     write ("hello,");
     writeline ("fanguzai!");
     //start up
  thread.start ();

Parameter range of anonymous method

Scope of anonymous method parameters:anonymous method block.

If the target is outside the block,The keyword you use inside an anonymous block-a jump keyword (like goto/break/continue)-is wrong;If the target is inside a block,The keyword you use outside of an anonymous block-like goto/break/continue-is also wrong.

If the scope of local variables and parameters already contains anonymous method declarations,The local variable and parameter will then be called the "external" (outside) variable of the anonymous method. You see, num in this snippet is an external (external) variable:

 int num=250;
  mydel del=delegate () {writeline ("#:{0}", --num);};

The referenced num of this external variable will be considered to be captured when the delegate is created,It's different from our common local variables,The lifetime of this external variable-the delegate object that references the anonymous method is reclaimed by .net's clr garbage collection mechanism.

[Note] Anonymous methods cannot access parameters in the external (external) scope with the ref and out keywords.

[Note] Unsafe code cannot be accessed in anonymous blocks.

[Note] To the left of the is operator,There is no need for anonymous methods.

Delegated example

The following example,When calling the delegate using both anonymous and named methods:

//declare delegate
 delegate void mydel (string s);
 class myclass
  static void main ()
   //instantiate delegate using anonymous method
   mydel p=delegate (string msg)
    console.writeline (msg);
   //call directly
   p ("Use an anonymous method to call the delegate.
   //Shorten the above form again
   console.writeline ("short form above");
   //Delegate instantiation of a static method using "do".
   p=new mydel (myclass.do);
   //call delegate using old style
   p ("Use a named method to call the delegate.
   console.read ();
  //used for delegate calls later
  static void do (string msg)
   console.writeline (msg);
  • Previous C # method for searching TreeView child nodes and retaining parent nodes
  • Next Introduction to special symbols commonly used in batch processing