What's new in C# 3.0 ? Part 5
Lambda Expressions : -
in earlier versions of C# when we were using delegate it was like that : -
suppose the delegate for multiply function.
in C# 1.0
first , i would create a delegate of type int
delegate int MathOp ( int x , int y ) ;
MathOp Multiply = new Mathop ( Multiply) ;
Console.Write(Multiply( 2 , 3 ) ;
Sure first you must make function Multiply :D
Private int Multiply ( int x , int y )
{
return x*y;
}
then it became more easy with the anonymous methods in C# 2.0
if i will use this method for once i don't have to declare methods and ....
anonymous methods allow us to make it as a block of code like this :-
delegate int MathOp ( int x , int y ) ;
MathOp Multiply = delegate ( int x , int y )
{
return x*y;
}
Console.Write(Multiply( 2 , 3 );
then it became more and more easy with Lambda Expression in C# 3.0
MathOp Multiply = ( x , y) => x*y;
Console.Write(Multiply( 2 , 3 );
So whats going on ?
on the left hand side : - we wrote the declared delegate and the name of the function
on the right hand side : - we wrote the parameters which we will apply the operation on them followed by the arrow followed by the operation.
hint : - if the code more than one line make them in a scope by { }.
this was the last post about C# 3.0 hope it was useful for you, and c u in another topic Soon isA.
No comments:
Post a Comment