Lambda Expressions

Lambda expressions is an anonymous function used to transform data and create shorter representations of normal function as delegates. In c# we use => operator to transform data. This subject is strongly related to delegates, I suggest you reading the delegates post first. Let’s take a look at syntax:

(input parameters) => expression or statement block

We pass lambda expressions as arguments of functions.

Now lets create an example of same function with standard syntax and lambda expression.

static bool LessThanTen(int number)
return n<10;

This function takes an integer value and returns true if the value is lower than 10.

We can declare this method as a delegate

delegate bool del(int n);
static void Main(string[] args)
    del LessThanTem = n => n<10;
    bool j = LessThanTen(5); //j = true

In this example we can see simple construction lambda expression syntax.

We can also pass the same function as a parameter in other function.

Static void PrintResut(number, del newDel){
Console.Writeline("condition is true")

else if(!newDel(number))
Console.Writeline("condition is false")

This construction allows us to pass different methods to the functions. Lets call one of them

PrintResult( 5, n => n < 10);

the result of this method is :

"condition is true"

This was just a brief introduction to idea of lambda expressions, in next post I’ll cover topic of expression trees. Stay Awesome!

Author: indiedevart

Indie Games Developer

2 thoughts on “Lambda Expressions”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google 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 )

Connecting to %s