LAMBDA EXPRESSION

Lambda Expressions

A lambda expression is a short block of code which takes in parameters and returns a value.
Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
Lambda expressions are any expression that uses the operator =>, which we read aloud as “goes to”.

For example – num => num * 7

Here, num is an input parameter and num * 7 is a return value. The lambda expression does not execute on its own. Instead, we use it inside other methods or variables.

The Lambda Expressions can be of two types:

  1. Statement Lambda: Consists of the input and a set of statements to be executed.

Syntax:-
input => { statements };

Note: Does not return any value implicitly

  1. Expression Lambda: Consists of the input and the expression.

Syntax:-
input => expression;

Note: Returns the evaluated value implicitly

Lambda Expression in asp. net:-

C# 3.0 introduced the lambda expression. It is also works like an anonymous method.
The difference that in Lambda expressions you don’t need to specify the type of the value that you input thus making it more flexible to use. It means lambda expression simplifies the anonymous function or you can say that lambda expression is a shorthand for an anonymous function.
The => is the lambda operator which is used in all lambda expressions.
The Lambda expression is divided into two parts, the left side is the input and the right is the expression.

Use of Lambda Expression

Some of the uses of the lambda expression are:

  1. Writing Easy and Simple Delegate Code
    Using lambda expressions, we can write much easier and simpler code. Let’s see programs with and without using a lambda expression in a delegate.

Program Without Using Lambda Expression

In the above program, we have defined a delegate square of Func type that points to the Square() method.

Program With Using Lambda Expression

Here, we don’t need to define a separate method. We have replaced the pointer to the square() method with the lambda expression.
  1. Passing Parameter in Method
    We can pass a lambda expression as a parameter in a method call.

Let’s take a built-in Count() method of C# array and pass a lambda expression as its parameter.

In the above example, we have passed the lambda expression x => x == 13 as a method parameter.

Output-

The Count() method checks each element of the numbers array and counts the total number of 13 in the array.

Lambda Expression in Blazor-

Blazor is a web framework developed by Microsoft that allows developers to build web applications using C# and .NET instead of JavaScript. In ASP.NET, Blazor is typically used to build web applications with server-side rendering. Here are some examples of how lambda expressions can be used in Blazor in ASP.NET:

Event handlers:
In Blazor, you can use lambda expressions to define event handlers for components. For example: To handle an event in Blazor, you can use a lambda expression to define a method that will be called when the event occurs. Here’s an example of how to handle the “onclick” event of a button:

In the above code, the lambda expression () => HandleClick() defines a method that takes no parameters and calls the HandleClick() method when the button is clicked.

Data binding:
In Blazor, you can use lambda expressions for data binding to components. For example: you can bind a string variable to a component’s text property using a lambda expression:

Here, the lambda expression @(() => MyProperty) is used to bind the value of an input element to the MyProperty property.