C# and .NET interview questions: – Explain Lambda Expression in C#?

Lambda expression: – A Lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

In simple words Lambda expression is nothing but it is an extended version of delegates or in other words it making delegates simpler.

All Lambda expression uses the lambda operator (=>).

The left side of the lambda operator specifies the input parameters (if any) and the right side hold the expression or statement block.

Let’s create a simple example to understand the concept of Lambda expression in much better manner.

In order to see it practically you just need to follow the following steps.

Step1: – Create a simple Console Application for that just open Visual Studio >> go to >> File >> New >> Project >> Windows >> Select Console Application.


Step2: – Now, simply just add the below code snippet in to program.cs file of your Console Application.

class Program
    {
        //created a delegate.
        delegate void PointToCallMe(string str);
        static void Main(string[] args)
        {
            //Delegate,which points to the CallMe Function.
            PointToCallMe objDelegate = new PointToCallMe(CallMe);
            objDelegate.Invoke("Called from Delegate");
            Console.ReadLine();
        }
        public static void CallMe(string str)
        {
            Console.WriteLine(str);
        }
    }

In the above code snippet you can see that I have created a function CallMe with a string parameter “str” and I have created a delegate which points toward the CallMe function.

Now, simply just run your console application and you should see the result as shown in the below diagram.

The above code is nice it work’s properly but the concern here is the code complexity; you can see that for a simple line of output we have created extra function, which makes your code little lengthy and not very easy to read.

So Lambda Expression helps us to solve the above problem in simplified manner and making our code more readable and understandable.

Let’s see how exactly Lambda Expression help to solve the above problem for that you just need to follow the following step.

Step3: – Now, just simply modify the program.cs file like below code snippet.

    class Program
    {
        //created a delegate.
        delegate void PointToCallMe(string str);
        static void Main(string[] args)
        {
            //Lambda Expression.
            PointToCallMe objLambda = str => Console.WriteLine(str);
            objLambda.Invoke("Called from Lambda");
            Console.ReadLine();
        }
    }

Now, in the above code snippet you can see that I have just eliminated the extra created CallMe function and just written a few line of Lambda Expression, which makes your code more readable and understandable with a few line of code.

Now, simply just run your console application and will see result like below output diagram.


Watch the following interesting video on threading in C#.NET: –

Get our more important .NET interview questions materials.

Regards,

Visit more author’s other Most asked .NET interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s