Lambda expression – How to start with the basic of Lambda expression
Posted on 8/27/2007 5:45:01 AM
in #Asp.Net 3.X
Hi,/P>
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. /SPAN,/P>
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,/P>
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.,/P>
A Lambda is expression is created by providing the parameter and then the => keyword and then the expression. ,/P>
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./P>
/SPAN>private bool ISGreaterThanZero(int i)/P>
/SPAN>{ /SPAN>/P>
/SPAN> /SPAN>if (i>0)/P>
/SPAN> /SPAN>{/P>
/SPAN> /SPAN>return true;/P>
/SPAN> /SPAN>}/P>
/SPAN> /SPAN>else/P>
/SPAN> /SPAN>{/P>
/SPAN> /SPAN> /SPAN>return false;/P>
/SPAN> /SPAN>}/P>
/SPAN>}/P>
The same function can be written as a Lambda expression in one line. Here is the Lambda for the same./P>
(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,/P>
Vikram/P>/SPAN>
|
Posted on 5/15/2008 3:12:44 AM
Hi Vikram,
/p>
How can it be (int i => i < 0); i think it should be (int i => i > 0);
..if i am wrong please explain...
/p>
I liked all ur posts.....
/p>
|
Posted on 5/15/2008 9:04:08 PM
HI Sri,
/p>
Good catch, It should be (int i => i > 0);
/p>
|