Anonymous Methods in C#

Hi guys, I hope everyone is fine. Me too. If you have opened this article with a little interest then I will assume you are a .NET learner (I don't want to make you feel bad by saying learner, everyone is in the same boat).

Anyway, let's start with our topic, what anonymous functions are in C#. Let's understand the generic concept of Anonymous. It means, the objects that don't have a strong identity. If you are an experienced JavaScript developer then you might have a solid understanding of Anonymous functions because every now and then we attach an anonymous function to a variable. The concept of anonymous methods was introduced in C# 2.0. An anonymous method is an inline unnamed method in the code. We can say, an anonymous method has only a body without a name, optional parameters and return type.

Here are a few features of anonymous methods.

  • A variable declared outside the anonymous method can be accessed inside the anonymous method.
  • A variable declared inside the anonymous method can't be accessed outside the anonymous method.
  • We use anonymous methods in event handling. For example, we can attach one anonymous function to a click event of a button.
  • An anonymous method is declared without parenthesis and can be assigned to a delegate with any signature.
  • An anonymous method can't access the ref or out parameters of an outer scope.

We can define an anonymous function using a delegate. In this example we have implemented a small anonymous function that will attach to a delegate object and the function will take one argument. The point to be made here is that since we are attaching an anonymous function with a delegate, we need to call it using a delegate. Have a look at the following example.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data.SqlClient;

using System.Data;

using System.Collections.ObjectModel;

 

namespace FuncTest

{

    class Program

    {

        //Declare a delegate

        delegate void mydel(int value);

        static void Main(string[] args)

        {

            //Attach anonymous function to delegate object

            mydel d = delegate(int value)

            {

                Console.WriteLine("Value is : " + value);

            };

            //Call to anynymous function using delegate

            d(100);

 

            Console.ReadLine();

        }

    }

}

Here is sample output of this example.

output

Here is the declaration of a delegate that will point to such a function whose return type is void and will take one argument.

delegate void mydel(int value);

Within the Main() method, we are attaching the anonymous function with the object of the delegate. This is the process.

mydel d = delegate(int value)
{
    Console.WriteLine("Value is : " + value);
};

In theory, anonymous functions as such don't have an identity. In this example also we are seeing that the function doesn't have a name, only a body.

Fine, if we now want to execute the anonymous function then we can call it using a delegate as in the following.

//Call to anynymous function using delegate
d(100);

In this example we have declared a single anonymous function. The beauty of anonymous functions is that we can define one function within another. Here is a sample example.
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data.SqlClient;

using System.Data;

using System.Collections.ObjectModel;

 

namespace FuncTest

{

    class Program

    {

        delegate void outer();

        delegate void inner();

        static void Main(string[] args)

        {

            outer outr = delegate()

            {

                Console.WriteLine("I am outer delegate");

                inner innr = delegate()

                {

                    Console.WriteLine("I am inner delegate");

                };

                innr();

            };

            outr();

 

            Console.ReadLine();

        }

    }

 

The following is output of the above example.

Result

Conclusion

In this article we learned how to implement an aynonymous function in C#. I hope you have liked it, if it is now to you.


Similar Articles