Understand how to use Lambda Expression in C#.

Suleiman Abdullah
4 min readOct 17, 2024

--

Objective: Understand how to use Lambda Expression in C#.

When we use lambda expression we create a function without name which is known in C# 2.0 as anonymous method.

This mean a lambda expression is the improved anonymous function but with better syntax. Which came later in C# 3.0 and late version of C#

When we create a lambda expression it is converted to one of delegate type according to the reference method it refer to.

Lets say we have a delegate of type of action named Damage, to create a lambda expression we set our delegate equal to then we write the lambda expression.

Note: A lambda expression can return something or execute a block of some statement. This example this method only run a single statement.

When we create a lambda expression a double parenthesis is the method body without name, and you can decide to provide a parameter or not. When you refer some variable of delegate to lambda expression our lambda expression automatic detect the data type of parameter you specify.

The Equal sign and greater sign means go to ,this mean after you provide a parameter will run the statement that follow.

Using Lambda operator for normal method without body.

Use lambda operator to allow method to run statement in a single line without define a body.

Note: In this one we use lambda operator to tell the method to go to /run a statement without define anonymous method.

Using lambda Expression for Return method.

When we have a return type make sure the return value match the return type of the method.

Return type must match the return value avoid this error.

Lambda Expression is the anonymous method “ (amount) => amount = amount -1; ” is equivalent to below method but the method will have a name.

Assigning a value/method signature to Lambda Expression/anonymous method.

In order to assign the variable to anonymous method, the value must be a delegate type.

To explain this here is the part of Microsoft documentation explain more clear reason why a value must be delegate type.

The types of its parameters and return value define the delegate type to which a lambda expression can be converted. If a lambda expression doesn’t return a value, it can be converted to one of the Action delegate types; otherwise, it can be converted to one of the Func delegate types. For example, a lambda expression that has two parameters and returns no value can be converted to an Action<T1,T2> delegate. A lambda expression that has one parameter and returns a value can be converted to a Func<T,TResult> delegate.

Link https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions

Using lambda Expression to Run multiple block of code with action delegate.

To run multiple line of code after go to sign we need to add curly bracket and close it with semi colon after close curly bracket and run our code in there.

If your lambda need to run multiple code block but it return something. You need to use return keyword after you execute your statement inside a curly bracket.

Note: But your delegate must be a return delegate whether a func<T,TResult> or traditional return delegate. Please read about delegate before read this article.

See you into the next one.

--

--

Suleiman Abdullah
Suleiman Abdullah

Written by Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development

No responses yet