Lambda Expression in C#

Lambda Expression in C# with real-time examples

In this article, I am going to discuss the Lambda Expression in C# with some examples. The Lambda Expression in C# is a shorthand for writing the anonymous function. So we can say that the Lambda Expression in C# is nothing but to simplify the anonymous function in C#. So please read our previous article before proceeding to this article where we discussed the anonymous function in C# with examples.

Let’s understand Lambda Expression in C# with an example.

namespace LambdaExpressionDemo
{
    public class LambdaExpression
    {
        public delegate string GreetingsDelegate(string name);

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

        public static string Greetings(string name)
        {
            return "Hello @" + name + " welcome to Dotnet Tutorials";
        }
    }
}
OUTPUT:

Lambda Expression in C#

In the above example, we use a delegate to bind a function. Let’s convert the above example to use an Anonymous function.

namespace LambdaExpressionDemo
{
    public class LambdaExpression
    {
        public delegate string GreetingsDelegate(string name);

        static void Main(string[] args)
        {
            GreetingsDelegate obj = delegate (string name)
            {
                return "Hello @" + name + " welcome to Dotnet Tutorials";
            };

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

Lambda Expression in C#

Questions: Two things come to our mind.

As Anonymous method are used for lesser writing, then why we used the delegate keyword. As delegate knows about the return type and parameter type it accepts, then why we explicitly specifying the parameter type the delegate accept.

Lambda Expression in C#

We can overcome this by using Lambda Expressions which is introduced in C#3.

To create a lambda expression in C#, we need to specify the input parameters (if any) on the left side of the lambda operator =>, and we need to put the expression or statement block on the other side.

Let rewrite the same example by using Lambda Expressions in C#.
namespace LambdaExpressionDemo
{
    public class LambdaExpression
    {
        public delegate string GreetingsDelegate(string name);

        static void Main(string[] args)
        {
            GreetingsDelegate obj = (name) =>
            {
                return "Hello @" + name + " welcome to Dotnet Tutorials";
            };

            string GreetingsMessage = obj.Invoke("Pranaya");
            Console.WriteLine(GreetingsMessage);
            Console.ReadKey();
        }

        public static string Greetings(string name)
        {
            return "Hello @" + name + " welcome to Dotnet Tutorials";
        }
    }
}
OUTPUT:

Lambda Expression in C#

Lambda Expression in C#

In the next article, I am going to discuss the Generic Delegates in C# with real-time examples

SUMMARY:

In this article, I try to explain Lambda Expression 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 *