c sharp

What is Lambda Expression and Anonymous Function in C#

Lambda expressions and anonymous functions are two powerful concepts in C# that allow developers to write concise, efficient, and easy-to-read code. These features allow developers to write inline functions without the need to declare a separate method. This article will discuss what lambda expressions and anonymous functions are in C# and provide separate examples for each.

What is Lambda Expression in C#

A Lambda expression is a short, concise way to define a method inline, without the need to declare a separate method. It is essentially an anonymous method that can be assigned to a variable or used as a parameter, Lambda expressions in C# are represented by the “=>” operator, which is read as “goes to” operator:

The syntax for a Lambda expression is:

(parameter) => expression

Where the parameter is the input to the function, and the expression is the output of the function. The following example demonstrates the use of a Lambda expression to calculate the square of a number:

int square = (x) => x * x;

In this example, the Lambda expression takes an input parameter x and returns the square of x. The result of the Lambda expression is assigned to the variable square and here is the full code for this example:

using System;

class Program {

  static void Main(string[] args) {

Func<int, int> square = x => x * x;

Console.WriteLine(square(6));

}

}

In this example, we define a Lambda expression that takes an integer input parameter x and returns its square. The Func<int, int> type defines that the Lambda expression takes an integer input parameter and returns an integer value. We assign this Lambda expression to the square variable, and then we call it with the input value of 6 and the output will be 36.

What is Anonymous Function C#

The anonymous function is a function that is without a name and is a kind of Lambda expression which is defined and called inline, without declaring a separate method. Anonymous functions in C# are represented by the “delegate” keyword, which is used to declare a new method with a specific signature, the syntax for an Anonymous function is:

delegate (parameter) { expression }

Where the parameter is the input to the function, and the expression is the output of the function. The following example demonstrates the use of an Anonymous function to calculate the sum of two numbers:

delegate (int a, int b) { return a + b; }

In this example, the Anonymous function takes two input parameters x and y and returns the sum of f and g:

using System;

class Program {

  static void Main(string[] args) {

Func<int, int, int> sum = delegate (int f, int g) { return f + g; };

Console.WriteLine(sum(2, 3));

}

}

In this example, we define an Anonymous function that takes two integer input parameters x and y and returns their sum. We declare the sum variable as a Func<int, int, int> type, which specifies that the function takes two integer input parameters and returns an integer value. We use the delegate keyword to define the function, and then we call it with the input values of 2 and 3. The output will be 5.

Conclusion

Lambda expressions and Anonymous functions are powerful concepts in C# that allow developers to write concise, efficient, and easy-to-read code. Lambda expressions are used to define inline methods without the need to declare a separate method, while Anonymous functions are used to define and call inline functions without the need to declare a separate method delegate (int x, int y) { return x + y; }. Both concepts are essential tools for any C# developer looking to write efficient and maintainable code.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.