C# Lambda Expressions with programming Examples

Description:

C# Lambda Expressions with Programming Examples: In this article, I am going to show you how to use lambda expressions in C# with step by step explanations.

C# Lambda expressions

With the delegates from C# 1.0 and 1.1, the language was given a way to generate pointers to code to be able to. C# 2.0 introduced anonymous methods to passcode pointers as arguments to methods. With the introduction of C# 3.5 the Anonymous method syntax has a clearer and simpler syntax. Furthermore, the C# lambda expressions create more possibilities, which are explained in this Article.


Introduction to C# Lambda Expressions

With the introduction of C# lambda expressions in C# 3.5, the syntax became more pronounced and simple.

C# Lambda Expressions Syntax:



Example1: how to use simple Lambda Expression in C#:

Output:

C# Lambda Expressions

The delegate definition myDelegate(int i, int j) makes it clear that the parameters to be transferred from Type are int. Consequently, it is possible to change the expression syntax by dispensing with the redundant type information to simplify.

myDelegate add = (i, j) => {return i + j; };

If, as in the above example1, there is only one return statement in the expression body, even the curly brackets and the keyword return in the expression are also omitted.


Using Func Delegate Types

With the keyword delegate, you can define any number of your own delegates, which may also be generic, can be

Since the introduction of LINQ, there have also been various predefined delegate types (namespace System.Linq). The delegate Func <> is four times overloaded and supports up to four parameters and one Return parameters.

TR defines the return type and is always last in the generic type list. Set T1 to T4 represents the different types of parameters. With the overload for a parameter you can do the above Rewrite the code example as follows:

Example2: how to use delegate type Func <> in C# :

Output:

C# Lambda Expressions


Predicates and projections Uses  in C# Lambda Expressions:

There are basically two types of C# lambda expressions: predicates3 and projections. A predicate delivers always returns true or false and expresses that a Boolean expression meets a condition or Not. A projection is a type of expression that differs from the parameter types.

Example3: how to use predicates of a C# lambda expressions:

Output:

C# Lambda Expressions

Example4 shows both types of C# lambda expressions again, using the predicate as a filter while the projections, such as s.ToUpper(), additional expressions or operations To run.


Example4:  how to use Projection of a C# lambda expressions:

Output:

C# Lambda Expressions

To emphasize once again the connection to the anonymous methods: instead of the predefined Func delegates one could also use the following definitions:



C# lambda expressions trees:

It is possible to represent a C# lambda expressions as an expressions tree and, if necessary, to manipulate it. An expression tree thus represents a representation of a C# lambda expressions. Around a lambda expression To be able to convert into an expression tree, you need the namespace System.Linq.Expressions with the generic type Expression <T>.

Example5: Reading out an expression tree:

Output:

C# Lambda Expressions

Example5 defines an expression for: p less than 7. You can use such a C# lambda expressions as in the This example can be executed by calling the Compile method or decomposed into its expression tree.

Conversely, it is also possible to generate a C# lambda expressions at runtime from an expression tree:


Example6: Generate C# lambda expressions using expression tree:

Output:

C# Lambda Expressions

Here, the functionality of the code from Listing example5 is illustrated by different expression definitions (ParameterExpression, ConstantExpression and BinaryExpression) recreated at runtime. About the generic Type Expression<T> the C# lambda expressions is generated in lambdaExpression and via the method Compile() executed. Table1 lists the most important expression classes for generation of an expression tree.


Classes for creating and displaying expression trees:

Table 1:

Type Description
Expression

Expression <T>

Abstract base class of all expression classes.

The generic class that manages an expression tree and the C# lambda expressions in binary

represents.

BinaryExpression BinaryExpression is used for expressions with two operands that the property values Own Left and Right.
ConditionalExpression The ConditionalExpression class is used to create a condition in an expression tree Are defined.
ConstantExpression The ConstantExpression class allows you to set a constant value that is contained in an Expression is used.
LambdaExpression This class represents a C# lambda expression that can be executed.
MethodCallExpression MethodCallExpression is used for expressions that represent a method call.
ParameterExpression This class defines the parameters that are used in an expression.
UnaryExpression UnaryExpression defines a unary expression that is contained in a C# lambda expressions is used.
TypeBinaryExpression This class defines an operation between an expression and a type.

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: