Please enable Javascript to correctly display the contents on Dot Net Tricks!

C Sharp Anonymous Method

  Author : Shailendra Chauhan
Posted On : 18 Jun 2012
Total Views : 107,969   
Updated On : 24 Sep 2016
 

The concept of anonymous method was introduced in C# 2.0. An anonymous method is inline unnamed method in the code. It is created using the delegate keyword and doesn’t required name and return type. Hence we can say, an anonymous method has only body without name, optional parameters and return type. An anonymous method behaves like a regular method and allows us to write inline code in place of explicitly named methods.

A Simple Anonymous Method Example

delegate int MathOp(int a, int b);
class Program
{
 //delegate for representing anonymous method
 delegate int del(int x, int y);

 static void Main(string[] args)
 {
 //anonymous method using delegate keyword
 del d1 = delegate(int x, int y) { return x * y; };

 int z1 = d1(2, 3);
 
 Console.WriteLine(z1);
 }
}
//output:
6

Key points about anonymous method

  1. A variable, declared outside the anonymous method can be accessed inside the anonymous method.

  2. A variable, declared inside the anonymous method can’t be accessed outside the anonymous method.

  3. We use anonymous method in event handling.

  4. An anonymous method, declared without parenthesis can be assigned to a delegate with any signature.

  5. Unsafe code can’t be accessed within an anonymous method.

  6. An anonymous method can’t access the ref or out parameters of an outer scope.

Anonymous Method as an Event Handler

 <form id="form1" runat="server">
 <div align="center">
<h2>Anonymous Method Example</h2>
 <br />
 <asp:Label ID="lblmsg" runat="server" ForeColor="Green" Font-Bold="true"></asp:Label>
 <br /><br />
 <asp:Button ID="btnSubmit" runat="server" Text="Submit" />  
 <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
 </div>
 </form> 
 protected void Page_Load(object sender, EventArgs e)
 {
 // Click Event handler using Regular method
 btnCancel.Click += new EventHandler(ClickEvent);
 // Click Event handler using Anonymous method
 btnSubmit.Click += delegate { lblmsg.Text="Submit Button clicked using Anonymous method"; };
 }
 protected void ClickEvent(object sender, EventArgs e)
 {
 lblmsg.Text="Cancel Button clicked using Regular method";
 } 
Summary

In this article I try to expose anonymous method with simple example. I hope after reading this article you will be able to use anonymous method in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.



Free Interview Books
 
COMMENTS (0)
6 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM Weekend

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+