Lambda expressions

With C# 3.0, Lambda expressions were introduced and are widely used in invoking delegates. Lambda expressions are created using Lambda operators. On the left-hand side of the operator, we specify the input parameters, while on the left-hand side, we specify the expression or code block. When a Lambda operator is used in an expression body, it separates the member's name from the member's implementation.

The Lambda operator is represented as a => token. This operator is right-associative and has the same precedence as an assignment operator. An assignment operator assigns a right-hand operand value to a left-hand operand.

In the following code, we are using a Lambda operator to compare a specific word in a string array and return it. Here, we are applying a Lambda expression to each element of the words array:

words.Where(w => w.Equals("apple")).FirstOrDefault();

This example also shows how we can use a LINQ query to get the same output. 

We are trying to find "apple" from an array of words using a LINQ query. Any enumerable collection allows us to query using LINQ and returns the desired output:

public void LambdaOperatorExample()
{
string[] words = { "bottle", "jar", "drum" };
// apply Lambda expression to each element in the array
string searchedWord = words.Where(w =>
w.Equals("drum")).FirstOrDefault();
Console.WriteLine(searchedWord);
// Get the length of each word in the array.
var query = from w in words
where w.Equals("drum")
select w;

string search2 = query.FirstOrDefault();
Console.WriteLine(search2);
}

//Output:
drum
drum

A Lambda expression is the right-hand side operator of a Lambda operator and is widely used in expression trees.

More information on expression trees can be on the Microsoft documentation website.

This Lambda expression must be a valid expression. If the member type is void, it's classed as a statement expression.

From C# 6 onward, these expressions support method and property get statements, while from C# 7 onward, these expressions support constructors, finalizers, property set statements, and indexers.

In the following code, we are using an expression to write the first name and last name of the variable and we have also used the Trim() function:

public override string ToString() => $"{fname} {lname}".Trim();

With this basic understanding of Lambda expressions and the Lambda operator, we can move on and look at how we can use Lambda expressions to invoke a delegate.

Recall that a Lambda expression can be represented like so:

Input-Parameters => Expression

In the following example, two extra lines have been added to the existing method to invoke the delegate using a Lambda expression. X is the input parameter, where the type of X is identified by the compiler:

delegate void StringDelegate(string strVariable);
public void InvokeDelegatebyAnonymousFunction()
{
//Named Method
StringDelegate StringDel = HelperClass.StringMethod;
StringDel("Chapter 5 - Named Method");

//Anonymous method
StringDelegate StringDelB = delegate (string s) { Console.WriteLine(s); };
StringDelB("Chapter 5- Anonymous method invocation");

//LambdaExpression
StringDelegate StringDelC = (X)=> { Console.WriteLine(X); };
StringDelB("Chapter 5- Lambda Expression invocation");

}

//Output:
Chapter 5 - Named Method
Chapter 5- Anonymous method invocation
Chapter 5- Lambda Expression invocation
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.188.201