FCI-Helwan blog

Just another FCI-H weblog

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 😀

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.

Advertisements

February 15, 2007 - Posted by | C#

No comments yet.

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

%d bloggers like this: