In this post we are discussing the new feature Lambda Expressions being introduced in c# 3.0 .
C# 3.0 introduces a new feature called lambda expressions. While this is not a revolutionary thing, it opens up a plethora of new possibilities for .Net programming. This post aims to introducing the Lambda Expressions by providing a background, the syntax for using it , and a suitable example.
A Little Bit of History
With the release of C# 2.0, came anonymous methods. For those of you who don’t know it, anonymous methods allow developers to write a block of code in places where a delegate is expected. Prior to this, developers are required to explicitly declare methods and attach them to delegates.
Just summing up over here , about delegates .
Delegates can be understood as a type safe object capable of pointing to another function (s) or method (s), or in very simple terms we can say function pointers.
Delegates do basically have 3 important pieces.
a. Name of method to which it is going to call or point
b. Arguments that will be passed
c. The return value .
In c# , delegate keyword is used while creating delegates .
ex : public delegate int mydeleg(int x);
Lambda Expression (Lambda expressions are simply delegate functions.(enhanced version))
In order to learn functional programming and a more declarative style of writing code, we need first to cover some basic material. One of the first concepts is that of lambda expressions. Lambda expressions can be summarized in one sentence:
Lambda expressions are simply functions/methods.
They have a different syntax, primarily so that they can be written in expression context (more on this shortly) instead of as a member of a class. However, that is all they are. For instance, the following lambda expression:
c => c + 1
is a function that takes one argument, c, and returns the value c + 1.
Actually, they are slightly more complicated than this, but not much more. For the purposes of this tutorial, you only use lambda expressions when calling a method that takes a delegate as a parameter. Instead of writing a method, creating a delegate from the method, and passing the delegate to the method as a parameter, you can simply write a lambda expression in-line as a parameter to the method.
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read “x goes to x times x.” This expression can be assigned to a delegate type as follows:
C#
delegate int del(int i);
static void Main(string[] args)
{
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
}
More about Lambda Expressions can be found here :
http://blogs.msdn.com/ericwhite/pages/Lambda-Expressions.aspx
http://msdn.microsoft.com/en-us/library/bb397687.aspx
VN:F [1.6.3_896]
Rating: 0.0/5 (0 votes cast)