SoFunction
Updated on 2025-03-08

Detailed explanation of C# anonymous delegation and Lambda expressions

By using anonymous delegation (anonymous method), programming becomes more flexible. For delegation and anonymous delegation, please refer to my previous blog.Entrustment and Anonymous Entrustment》。

Continuing with the previous example, the code is as follows:

static void Main(string[] args)
{

   method = delegate(int a, int b)
 {
  return a + b;
 };
  Worker worker = new Worker();
  int result = (10, 10,method);

  (("Result:{0}", result));

  ();
}

The above program worker will calculate the result and return it according to the parameters and calculation method given by Main. According to the equivalence code, it can be further simplified as follows:

 static void Main(string[] args)
 {
  Worker worker = new Worker();
  int result = (10, 10, delegate (int a, int b)
  {
  return a + b;
  });

  (("Result:{0}", result));

  ();
 }

Friends who have experience in js and jquery development here may suddenly feel friendly, oh...anonymous functions. $("#id").click(function(){.........}) is used everywhere in jquery.

In C#, anonymous delegates can be further simplified when used, and the result of being simplified becomes an expression, called a Lambda expression.

static void Main(string[] args)
 {

   method = delegate(int a, int b)
 {
  return a + b;
 };
}

Lambda expressions:

static void Main(string[] args)
 {

   method = (a, b)=>
 {
  return a + b;
 };
}

C# Lambda expressions are divided into two parts: first and last using =>, (a, b) is the parameter list, and {....} is the method body.

 static void Main(string[] args)
 {
  Worker worker = new Worker();
  int result = (10, 10, (a, b) =>
  {
  return a + b;
  });

  (("Result:{0}", result));

  ();
 }

The following is a comparison of the method and Lambda expression conversion

private void A1()
  {
   ("....");
  }
//Lambda
()=>{("....");};
//If the method body code only has one sentence, the method body braces can be omitted as above()=>("....");
private string A2(int a, int b)
  {
   return ("{0}+{1}={2}", a, b, a + b);
  }
  //The example above can be simplified to  (a,b)=>{return ("{0}+{1}={2}", a, b, a + b);}
  //Because the method body only has one sentence, it can be further simplified  (a,b)=> ("{0}+{1}={2}", a, b, a + b);
  //Attention nowreturnIt should be removed,C#Compiler automatically recognizes => Do return value
private string A3(int a)
  {
   return ("{0}", a);
  }
  //The above example can be simplified to  (a)=>("{0}", a);
  //If there is only one parameter in the parameter list, the () of the parameter list can be removed, and the above is further simplified to  a=>("{0}", a);

Through this article, you may have realized that in C#, methods, delegates, anonymous methods, lambda expressions do not clearly distinguish boundaries, and they can be transformed flexibly. There are many knowledge points about delegation in actual development, such as delegation and events. If time allows, I hope to write a post that deepens the commission.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.