C Sharp Anonymous Method

C Sharp Anonymous Method

19 Sep 2025
Intermediate
44.2K Views
7 min read
Learn with an interactive course and practical hands-on labs

Best Free C# Course: Learn C# In 21 Days

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.

To master this concept and other C# techniques, you can enroll in a C Sharp Online Course Free and enhance your skills today!

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

  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 an anonymous method in event handling.

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

  5. The 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>   
Anonymous Method example
  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";
 }  
Anonymous Method example
Anonymous Method example
Read More Articles Related to csharp
Summary

Anonymous types in C# provide a quick and convenient way to create read-only objects without explicitly defining a class. They are especially useful when working with LINQ queries, where you often need to project only a subset of properties. By using anonymous types, developers can write more concise, readable, and efficient code without cluttering the solution with extra class definitions.

With 15,000+ .NET full stack jobs open in 2025, certified developers are in demand. Secure your role with our .NET full stack developer course now!

FAQs

Anonymous types are class types that derive directly from object , and that cannot be cast to any type except object 

The Anonymous Method is an inline code that can be used wherever a delegate type is expected.

An anonymous method in C# cannot contain any jump statement like goto, break or continue. Anonymous Method in C# cannot access the ref or out parameter of an outer method.

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.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

He is a renowned Speaker, Solution Architect, Mentor, and 10-time Microsoft MVP (2016–2025). With expertise in AI/ML, GenAI, System Design, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development, he bridges traditional frameworks with next-gen innovations.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. As a corporate trainer for leading MNCs like IBM, Cognizant, and Dell, Shailendra continues to deliver world-class learning experiences through technology & AI.
Live Training - Book Free Demo
ASP.NET Core Certification Training
25 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
25 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Solution Architect Certification Training
26 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Microservices Certification Training
26 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack Java Developer Certification Training Course
01 Nov
05:30PM - 07:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this