Lambda Expressions in C#

Lambda Expressions in C# with real-time Examples

In this article, I am going to discuss the Lambda Expressions in C# with some examples. Please read our previous article before proceeding to this article where we discussed the Anonymous Methods in C# with examples. As part of this article, we are going to discuss the following pointers in detail.

  1. What are Lambda Expressions in C#?
  2. Why do we need Lambda Expressions?
  3. How to create Lambda Expression in C#?
  4. Examples of using Lambda Expression.
What are Lambda Expressions in C#?

The Lambda Expression in C# is the 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#. Let’s understand this 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 Expressions 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 the Anonymous method is 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 accepts.

Lambda Expressions in C#

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

How to create Lambda Expression in C#?

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 us rewrite the same example by using Lambda Expression 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#

How to create Lambda Expression in C#?

In the next article, I am going to discuss the Generic Delegates in C# with real-time examples. Here, in this article, I try to explain Lambda Expressions in C# with some examples. I hope now you understood how to create and work with Lambda Expression in C#.

1 thought on “Lambda Expressions in C#”

Leave a Reply

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