The following program demonstrates an Example of Anonymous Functions in C#.

An anonymous function is a function that doesn’t have a name. In C#, an anonymous function is created with the help of a delegate. In order to define an anonymous function, first we create a delegate with an appropriate signature. After that, we create an object of the delegate and assign the function to the object of the delegate. Once, the anonymous function is defined, it is called using the delegate object.

The following program declares two delegates – del1, and del2. Since del1 doesn’t take any argument, we can use to refer to function with no argument. Similarly, del2 can refer to a function that takes an argument of integer type.

using System;
namespace AnonymousFunctionDemo
{
    class Program
    {
        delegate int del1();
        delegate int del2(int n);

        static void Main(string[] args)
        {
            while(true)
            {
                Console.WriteLine(@"Select
1. Find Sum
2. Find Square
3. Exit");
                Console.WriteLine("Enter your Choice: ");
                int ch = Int32.Parse(Console.ReadLine());

                switch(ch)
                {
                    case 1:
                        del1 ob1 = delegate ()
                        {
                            Console.WriteLine("Enter a Number: ");
                            int x = Int32.Parse(Console.ReadLine());
                            int sum = 0;
                            for(int i=0;i<=x;i++)
                            {
                                sum += i;
                            }
                            Console.Write("Sum of numbers from 1 to {0} is ", x);
                            return sum;
                        };
                        int s = ob1();
                        Console.WriteLine(s);
                        break;
                    case 2:
                        Console.WriteLine("Enter a number: ");
                        int y = Int32.Parse(Console.ReadLine());
                        del2 ob2 = delegate (int i)
                          {
                              int sq = 0;
                              for(int j=1;j<=i;j++)
                              {
                                  sq += i;
                              }
                              return sq;
                          };

                        int z = ob2(y);
                        Console.WriteLine("Square of {0} is {1}", y, z);
                        break;
                    case 3:Environment.Exit(0);
                        break;
                    default:Console.WriteLine("Wrong Choice Entered!");
                        break;
                }
            }
        }
    }
}

Output

Demonstrating an Example of Anonymous Functions in C#
Demonstrating an Example of Anonymous Functions in C#

Further Reading

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Princites

IITM Software Development Cell