22
DecC Sharp Anonymous Method
Anonymous Method: An Overview
The concept of the anonymous method was introduced in C# 2.0. An anonymous method is an inline unnamed method in the code. It is created using the delegate
keyword and doesn’t require a name and return type. Hence we can say, that an anonymous method has only a body without a 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. In this C# Tutorial, we will explore more about the C-Sharp Anonymous Method, which will include what is an Anonymous Method, and the Anonymous Method in C# with its example.
What is the Anonymous Method?
- An anonymous method is nothing but a method without a name, As the name suggests.
- Anonymous methods in C# can be defined using the delegate keyword
- And it can be assigned to a variable of delegate type.
- It can access variables defined in an outer function
A Simple Anonymous Method Example in C# Compiler
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
Read More - C Sharp Interview Questions
Key points about the anonymous method
A variable declared outside the anonymous method can be accessed inside the anonymous method.
A variable declared inside the anonymous method can’t be accessed outside the anonymous method.
We use an anonymous method in event handling.
An anonymous method declared without parenthesis can be assigned to a delegate with any signature.
The unsafe code can’t be accessed within an anonymous method.
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";
}
Read More Articles Related to csharp
Summary
In this article, I try to expose the anonymous method with a simple example. I hope after reading this article you will be able to use an anonymous method in your code. I would like to have feedback from my blog readers. Please post your feedback, questions, or comments about this article. Also, Consider our C# Programming Course for a better understanding of all C# concepts.
FAQs
Take our Csharp skill challenge to evaluate yourself!
In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.