Lambda expression – How to start with the basic of Lambda expression

Hi,

One of the new features of C Sharp 3.0 is Lambda expression. Lambdas are great new thing from language prospective. But for a lower or middle level developer they are not that simple. Actually a common Dot net, C Sharp, developer is not used to the kind of the expression. 

For a normal developer Lambda expressions will be a bit difficult in the beginning. But believe me when you have had a little bit of understanding on how to start with Lambda then You will understand the power of it,

In this post I will show on writing some of the Lambda expression. I hope these examples helps on understanding the Lambda Expression. Let me start with how a Lambda expression is made.,

A Lambda is expression is created by providing the parameter and then the => keyword and then the expression. ,

Lets say we want a function were we will pass the integer and the function will return back if the integer is greater than 0 ore not. Here is an example of the function.

            private bool ISGreaterThanZero(int i)

            {         

                if (i>0)

                {

                    return true;

                }

                else

                {

                    return false;

                }

            }

The same function can be written as a Lambda expression in one line. Here is the Lambda for the same.

(int i => i < 0);

The lambda expression does the same work as the function above. The good thing about the lambda expression is that they can be written inline and are very small and simple to be written,

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 5/15/2008 3:12:44 AM

Hi Vikram,

How can it be (int i => i < 0); i think it should be (int i => i > 0);
..if i am wrong please explain...

I liked all ur posts.....

Posted on 5/15/2008 9:04:08 PM

HI Sri,

Good catch, It should be (int i => i > 0);

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia