Anonymous Method in C#

Anonymous Method in C# with examples

In this article, I am going to discuss the Anonymous Method in C# with some examples. As the name suggests, an anonymous method in C# is a method without having a name. The Anonymous methods in C# can be defined using the keyword delegate and can be assigned to a variable of the delegate type.

In our Delegates in C# article, we discussed how to bind a delegate with a method. To bind a delegate with a method, first we need to create an instance of a delegate and when we create the instance of a delegate, we need to pass the method name as a parameter to the delegate constructor, and it is the function the delegate will point to.

Below is the example to understand Delegate in C#.
namespace DelegateDemo
{
    public class AnonymousMethods
    {
        public delegate string GreetingsDelegate(string name);
        public static string Greetings(string name)
        {
            return "Hello @" + name + " Welcome to Dotnet Tutorials";
        }

        static void Main(string[] args)
        {
            GreetingsDelegate gd = new GreetingsDelegate(AnonymousMethods.Greetings);
            string GreetingsMessage = gd.Invoke("Pranaya");
            Console.WriteLine(GreetingsMessage);
            Console.ReadKey();
        }
    }
}
OUTPUT:

Anonymous Method in C#

In the above example,

  1. We create one delegate (GreetingsDelegate)
  2. Then we instantiate the delegate, while we are instantiating the delegate, we are passing the Method name as a parameter to the constructor of delegate
  3. Finally, we invoke the delegate

As of now, this is the approach we are following to bind a method to a delegate and execute.

An anonymous method is also related to a delegate. Without binding a named block (function) to a delegate, we can also bind a code block to a delegate means an unnamed code blocked to a delegate which is nothing but an anonymous method in C#.

Let’s see an example for a better understanding of the Anonymous Method in C#.
namespace DelegateDemo
{
    public class AnonymousMethods
    {
        public delegate string GreetingsDelegate(string name);
        
        static void Main(string[] args)
        {
            GreetingsDelegate gd = delegate(string name) 
            {
                return "Hello @" + name + " Welcome to Dotnet Tutorials";
            };
            string GreetingsMessage = gd.Invoke("Pranaya");
            Console.WriteLine(GreetingsMessage);
            Console.ReadKey();
        }
    }
}
OUTPUT:

Anonymous Method in C#

In the above example, the following code is an example of an Anonymous method.

Anonymous Method in C#

The above code is without a name and it contains only the body and the method is defined using the delegate keyword. We don’t require writing any access modifiers like public, private and protected, etc. We also don’t require writing any return type like a void, int, double, etc.

Advantages of the Anonymous Method in C#:

Lesser typing word.  Generally, anonymous methods are suggested when the code volume is very less.

Anonymous methods can access variables defined in an outer function.

Let’s see an example for a better understanding.

namespace DelegateDemo
{
    public class AnonymousMethods
    {
        public delegate string GreetingsDelegate(string name);
        
        static void Main(string[] args)
        {
            string Message = "Welcome to Dotnet Tutorials";

            GreetingsDelegate gd = delegate(string name) 
            {
                return "Hello @" + name + " " + Message;
            };

            string GreetingsMessage = gd.Invoke("Pranaya");
            Console.WriteLine(GreetingsMessage);
            Console.ReadKey();
        }
    }
}
OUTPUT:

Anonymous Method in C#

Anonymous Method Limitations
  1. An anonymous method in C# cannot contain any jump statement like goto, break or continue.
  2. It cannot access the ref or out parameter of an outer method.
  3. The Anonymous method in C# cannot have or access the unsafe code.
Points to Remember while working with the Anonymous Method in C#:
  1. The anonymous method can be defined using the delegate keyword
  2. An anonymous method must be assigned to a delegate type.
  3. This method can access outer variables or functions.
  4. An anonymous method can be passed as a parameter.
  5. The anonymous method can be used as event handlers.

In the next article, I am going to discuss the Lambda Expression in C# with examples.

SUMMARY:

In this article, I try to explain the Anonymous Method in C# with some examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *