Reducing Code With Anonymous Functions

Anonymous functions, in a simple way, are functions that can be activated directly without declaring his name, as his own name says "anonymous".
 
This technic works in C# projects, .NET Framework, .NET Core or WPF and can be used to reduce code, on this POST I attached a sample of using anonymous X common (regular) way.
 
The final result is a reduced code and fast startup. 
 
 

The common way

 
Below is a regular code for fired Click events. To run this Form there is extra code in FormCommon.Designer.cs.
 
FrmCommon.cs
  1. using System;  
  2. using System.Windows.Forms;  
  3. namespace AnonymousEventsForClick  
  4. {  
  5.     public partial class FrmCommon : Form  
  6.     {  
  7.         public FrmCommon()  
  8.         {  
  9.             InitializeComponent();  
  10.         }  
  11.         private void menu1ToolStripMenuItem_Click(object sender, EventArgs e)  
  12.         {  
  13.             // Your code  
  14.             // CODE xyz  
  15.             var n = 0;  
  16.             APIProject.Print(++n);  
  17.         }  
  18.         private void checkBox1_CheckedChanged(object sender, EventArgs e)  
  19.         {  
  20.             APIProject.CheckStatus();  
  21.         }  
  22.         private void button1_Click(object sender, EventArgs e)  
  23.         {  
  24.             // Sample to access the object  
  25.             if (!(sender is Button)) return;  
  26.             APIProject.OpenDocument();  
  27.         }          
  28.     }  
  29. }  
FrmCommon.Desingner.cs
 
Extra code
 
...
this.menu1ToolStripMenuItem.Click += new System.EventHandler(this.menu1ToolStripMenuItem_Click);
...
this.button1.Click += new System.EventHandler(this.button1_Click);
...
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
...
 

The Anonymous way

 
Below is the same code with anonymous functions to fire events, with fewer functions, lines, and usings. There is no extra code in FrmAnonymous.Designer.cs
  1. using System.Windows.Forms;  
  2. namespace AnonymousEventsForClick  
  3. {  
  4.     public partial class FrmAnonymous : Form  
  5.     {  
  6.         public FrmAnonymous()  
  7.         {  
  8.             InitializeComponent();  
  9.             menu1ToolStripMenuItem.Click += (s, e) =>  
  10.             {  
  11.                 // You can add your code in the body  
  12.                 // CODE xyz  
  13.                 var n = 0;  
  14.                 APIProject.Print(++n);  
  15.             };  
  16.             checkBox1.CheckedChanged += (s, e) => { APIProject.CheckStatus(); };  
  17.             button1.Click += (s, e) =>              
  18.             {  
  19.                 // Sample to access the object  
  20.                 if (!(s is Button)) return;  
  21.                 APIProject.OpenDocument();              
  22.             };  
  23.         }  
  24.     }  
  25. }  

CONCLUSION

  
Using this technic you can reduce code and organize it in a single place.
 
Anonymous functions can be used to any kind of Events, this sample shows only for the Click event.
 
Happy coding.
Next Recommended Reading Anonymous Types In C#