C#

Anonymous Functions in C#

This article describes Anonymous Functions in C# with examples. Basically, anonymous functions are those functions that don’t have any name. Hence, we define the anonymous functions at that place in the code where we need to use them.

In fact, C# has a delegate keyword that we can use to create anonymous functions. In order to create an anonymous function, we must create a variable of the type delegate. Further, we need to create a function using the delegate keyword. After that, the function is assigned to the variable of the delegate type.

How to Create Anonymous Functions in C#

In order to create an anonymous function first, we need to create a delegate. The following syntax shows how to create a delegate.

delegate return-type delegate-name(parameter-list);

Examples:
delegate int mydelegate(int a, int b);
delegate String mydelegate1(String s1);

After that, we need to create a variable of the delegate type declared earlier. For example,

mydelegate v1, v2;
mydelegate1 x1, x2;

Now that we have variables of these delegate types, we can create anonymous methods of matching signatures and assign them to these variables. For example,

v1=delegate(int a, int b){
   //statements
}

Example Program to Demonstrate Anonymous Functions in C#

The following code shows how to create and use anonymous functions in C#. As can be seen, we have two delegate types. The first one returns a value of int type and takes no parameter. Likewise, the second delegate takes an integer type parameter and returns an integer type value.

Calling an Anonymous Method

In order to call an anonymous method, we just need to use the delegate variable name followed by the parentheses. Furthermore, any parameter should be enclosed in the parentheses separated by a comma. Also, assign the returned value in a variable of the appropriate type. For instance, consider the following method call

<return-type> <variable-name> = <delegate-variable-name>(<parameter-list>);

For example,

int x = mydelegate(5, 12);

The following code shows two anonymous functions that return the sum of numbers.

using System;

namespace AnonymousFunctionExample2
{
    delegate int d1();
    delegate int d2(int n);
    class Program
    {
        static void Main(string[] args)
        {
            d1 ob = delegate ()
            {
                int n = 5;
                int s = 0;
                for(int i=1;i<=n;i++)
                {
                    s += i;
                }
                return s;
            };

            int s1 = ob();
            Console.WriteLine("Sum = " + s1);
            d2 ob1 = delegate (int j)
            {
                int s = 0;
                for (int i = 1; i <= j; i++)
                {
                    s += i;
                }
                return s;
            };

            int s2 = ob1(7);
            Console.WriteLine("Sum = " + s2);
        }
    }
}

Output

The Output of a Program Demonstrating Anonymous Functions in C#
The Output of a Program Demonstrating Anonymous Functions in C#

You may also like...

Leave a Reply

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