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.
Syntax:
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.
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) { System.Console.WriteLine(j); }; obj("Delegate Using the Anonymous Method"); obj = new Print(TestClass.named); obj("Delegate Using the Named Method"); Console.Read(); } static void named(string k) { System.Console.WriteLine(k); } }
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.
Delegate Using the Anonymous Method Delegate Using the Named Method
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:
- We define the event delegate using the delegate keyword, specifying the event parameters (object sender and EventArgs e).
- Create an event using the defined delegate. Replace EventName with the desired name for your event.
- Subscribe to the event using an anonymous method. The code within the anonymous method block will execute when the event is triggered.
- 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.
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice MCA MCQs
- Apply for C# Internship
- Practice Computer Science MCQs
- Buy C# Books
- Buy MCA Books