C# Program to Create Anonymous Method


Anonymous method in C# are unnamed blocks of code defined inline within the code. They are used to pass short code blocks as arguments to other methods or for event handling, providing a concise way to define small logic blocks.

The syntax for defining an anonymous method in C# is as follows:

delegate returnType delegateName(parameters)
    // Code block for the anonymous method
    // ...

The returnType indicates the method’s return type, delegateName represents the anonymous method’s delegate, and parameters are the input parameters. The method’s logic is enclosed within the curly braces.

Creating Anonymous Methods

Here is source code of the C# Program to Create Anonymous Method. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Create Anonymous Method
using System;
delegate void Print(string s);
class TestClass
    static void Main()
        Print obj = delegate(string j)
        obj("Delegate Using the Anonymous Method");
        obj = new Print(TestClass.named);
        obj("Delegate Using the Named Method");
    static void named(string k)
Program Explanation

1. We define a delegate Print for methods that take a string parameter and return void.
2. In the Main method, we create an instance obj of the Print delegate using an anonymous method that prints the input string.
3. We invoke obj with “Delegate Using the Anonymous Method,” executing the anonymous method and displaying the message.
4. Then, we replace the anonymous method with the named method by assigning it to obj.
5. We invoke obj again with “Delegate Using the Named Method,” executing the named method and displaying the message.
6. The program waits for user input before ending.

Note: Join free Sanfoundry classes at Telegram or Youtube
Program Output
Delegate Using the Anonymous Method
Delegate Using the Named Method
Using Anonymous Methods in Event Handling

Here’s the syntax for using anonymous methods in event handling in C#:

// Define the event delegate
delegate void EventHandler(object sender, EventArgs e);
// Create an event using the delegate
event EventHandler EventName;
// Subscribe to the event using an anonymous method
EventName += delegate (object sender, EventArgs e)
    // Event handler code goes here
    // ...

In this syntax:

  1. We define the event delegate using the delegate keyword, specifying the event parameters (object sender and EventArgs e).
  2. Create an event using the defined delegate. Replace EventName with the desired name for your event.
  3. Subscribe to the event using an anonymous method. The code within the anonymous method block will execute when the event is triggered.
Advantages of Using Anonymous Methods
  • Eliminating Separate Methods: With anonymous methods, you can define small pieces of code directly at the point of use, eliminating the need to create separate methods.
  • Concise Syntax: The syntax for anonymous methods is compact, making code more readable and maintainable for short logic blocks.
  • Encapsulation of Logic: Anonymous methods allow encapsulation of logic without the overhead of defining a separate method.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.