Table of contents
1.
Introduction
2.
Anonymous Method in C#
3.
Key Points for Anonymous Method in C#
4.
Examples
4.1.
Code 1
4.2.
Code 2
5.
Frequently Asked Questions
5.1.
What are the advantages of using Anonymous Methods in C#?
5.2.
What are the limitations of Anonymous Methods in C#?
5.3.
How to use the Anonymous Method in C#?
6.
Conclusion
Last Updated: Mar 27, 2024

Anonymous Method in C#

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A method can be termed a code block which consists of a series of statements. A method is a piece of code that only executes when invoked. Data, known as parameters, can be sent into a method. Methods, often known as functions, carry out specific activities.

In this article, we shall be discovering the anonymous methods in C#. 

So, Without further delay, let us move to the actual topic.

Anonymous Method in C#

An anonymous method is a method that bears no name and was introduced in C# 2.0. This is handy when a user wishes to build an inline method and send parameters to the anonymous method like other methods. The delegate keyword is used to create an anonymous method, which the user may then assign to a delegate type variable.

Syntax

delegate(parameter_list){
//code…
};

Also Read About - singleton design pattern in c#

Key Points for Anonymous Method in C#

There are some key points to keep in mind whenever you are implementing the anonymous method in C#.

  • Inline delegate is another name for this technique.
  • This technique allows you to build a delegate object without writing many methods.
  • This method has access to the outer method's variables. Outer variables are a subset of such variables. 

Examples

Let us see a few code examples related to Anonymous Method in C#.

Code 1

using System;

public delegate void Show(string x);

class AnonymousMethodDemo{

public static void identity(Show mycode, string coder)
{
coder = " Coding" + coder;
mycode(coder);
}


static public void Main()
{
identity(delegate(string coder) {
Console.WriteLine("Coding Ninjas"+
" is one of the best institutions for {0}", coder); }, " and Learning.");
}
}

 

Output

Coding Ninjas is one of the best institutions for Coding and Learning.

In this example, we have created an anonymous function and printed some output.

Code 2

using System;

delegate void NumberChanger(int n);
namespace DelegateAppl {
   class TestDelegateDemo {
      static int num = 10;
      
      public static void AddNum(int p) {
         num += p;
         Console.WriteLine("Named Method: {0}", num);
      }
      public static void MultNum(int q) {
         num *= q;
         Console.WriteLine("Named Method: {0}", num);
      }
      public static int getNum() {
         return num;
      }
      static void Main(string[] args) {
       
         NumberChanger nc = delegate(int x) {
            Console.WriteLine("Anonymous Method: {0}", x);
         };
         nc(100);
         nc = new NumberChanger(AddNum);
         nc(15);
         nc = new NumberChanger(MultNum);
         nc(20);
         Console.ReadKey();
      }
   }
}

 

Output

Anonymous Method: 100
Named Method: 25
Named Method: 500

In this example, we have used the anonymous method. Here delegate calls both named and anonymous methods, in the same way, using the delegate object to pass the method parameters.

Frequently Asked Questions

What are the advantages of using Anonymous Methods in C#?

Anonymous methods in C# are less challenging to type. When there is a little code, anonymous methods are usually recommended.

What are the limitations of Anonymous Methods in C#?

A jump statement like goto, break, or cannot be used in an anonymous method in C#. An outer method's ref or out argument cannot be accessed. The anonymous methods cannot access or have access to the dangerous code.

How to use the Anonymous Method in C#?

We build a method with the same signature as the Predicate Generic Delegate. Then we build a Predicate Generic Delegate instance.

Then we send the Predicate Instance as a parameter to the List collection class's Find function.

Conclusion

In this article, we have covered the Anonymous Method in C#. We have introduced the topic along with why we need this method. We also discussed its key points. We have addressed the code implementation along with their output and explanation.

Enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you are just a beginner and want to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

We hope that this article might have helped you in enhancing your knowledge. If you liked this article, please give it a thumbs up!

“Happy Coding”!.

Live masterclass