Anonymous Functions in C#

Leave a comment

Anonymous functions in C# are basically functions without a name that can be used in different situations. Let’s take a look at some examples.

When looking at delegates, we saw how to pass delegate functions as parameters. With anonymous functions, we can define the delegate and then pass an anonymous function to it.

In the example below, we are creating a delegate and then creating the anonymous function code, then calling the delegate:

        public delegate void DoPrint(string value);

        static void Main(string[] args)
        {
            DoPrint p = delegate (string value)
            {
                Console.WriteLine("You entered: {0}", value);
                Console.ReadLine();
            };

            p("Hello World");
        }
    }

This produces:

Notice there is no name when defining the delegate code using the keyword delegate.

The delegate can also access variables outside of its code. Below, we have a string s defined, which is used within the function code:

            string s = " How are you?";

            DoPrint p = delegate (string value)
            {
                value += s;
                Console.WriteLine("You entered: {0}", value);
                Console.ReadLine();
            };

            p("Hello World.");

This produces:

Now, anonymous functions can themselves be passed as parameters to functions that reference delegates.

Consider the example. Below, we can call the delegate as we previously did:

        public delegate string DoPrint();

        static void PrintSomething(DoPrint print)
        {
            Console.WriteLine(print());
        }

        static string SayHello()
        {
            return "Hello";
        }

        static void Main(string[] args)
        {
            PrintSomething(SayHello);
            Console.ReadLine();
        }
    }

However, we can also call the delegate like this, where insert the function code directly into the parameter, thereby removing the need to create a separate function:

        public delegate string DoPrint();

        static void PrintSomething(DoPrint print)
        {
            Console.WriteLine(print());
        }        

        static void Main(string[] args)
        {
            PrintSomething(delegate ()
            {
                return "Hello";
            });
            Console.ReadLine();
        }

This produces:

This is useful if you don’t want to go through the work of creating an extra method.

Finally, we can call anonymous functions with event handlers, for example:

Button1.Click += delegate(Object o, EventArgs e) 
{
    MessageBox.Show ("Success"); 
};

 

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

See more articles on: C#

Leave a Reply

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