Mastering C# Lambda Expressions: A Complete Guide

letsmakelisting 10 views 2 slides Mar 11, 2025
Slide 1
Slide 1 of 2
Slide 1
1
Slide 2
2

About This Presentation

Lambda expressions in C# provide a concise way to write anonymous functions, making code more readable and expressive. This guide covers everything from the basics to advanced use cases, including how lambda expressions work, their syntax, real-world applications, and how they are used in LINQ, dele...


Slide Content

C# Lambda Expressions: A Complete Guide
C# Lambda Expressions are an essential feature of modern C# programming, allowing for more concise
and readable code. They are widely used in LINQ queries, event handling, and functional programming
paradigms. This guide covers everything you need to know about lambda expressions in C#.
What is a Lambda Expression in C#?
A lambda expression in C# is an anonymous function that can contain expressions and statements. It is
a more concise way to write inline functions, particularly useful when working with delegates and LINQ
queries.
Syntax of a Lambda Expression
The basic syntax of a lambda expression is:
(parameters) => expression
●The => operator, known as the lambda operator, separates parameters from the body of the
expression.
●If there is a single parameter, parentheses can be omitted.
●If there are multiple parameters or no parameters, parentheses are required.
●If the body contains a single expression, curly braces {} are optional.
●If the body contains multiple statements, it must be enclosed in {} and use an explicit return
statement if necessary.
Example of a Simple Lambda Expression
Func<int, int> square = x => x * x;
Console.WriteLine(square(5)); // Output: 25
Lambda Expressions with Delegates
A delegate is a type that holds references to methods. Lambda expressions are often assigned to
delegate types.
Example Using Func<T, TResult>
Func<int, int, int> add = (a, b) => a + b;
Console.WriteLine(add(3, 4)); // Output: 7

Example Using Action<T>
Action<string> greet = name => Console.WriteLine($"Hello, {name}!");
greet("John"); // Output: Hello, John!
Lambda Expressions with LINQ
Lambda expressions are heavily used in LINQ (Language Integrated Query) to filter, sort, and
manipulate data collections.
Example Using LINQ with Lambda Expressions
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (var num in evenNumbers)
{
Console.WriteLine(num); // Output: 2, 4, 6, 8, 10
}
Expression-bodied Methods
C# allows you to use lambda expressions for concise method definitions, known as expression-bodied
members.
public class MathOperations
{
public int Square(int x) => x * x;
}
Benefits of Lambda Expressions in C#
●Concise and Readable: Reduces boilerplate code.
●Improved Performance: Inline execution avoids the overhead of named method invocation.
●Better Maintainability: More intuitive and easier to read.
●Enhanced LINQ Queries: Simplifies data processing with functional programming techniques.
Conclusion
Lambda expressions are a powerful feature in C# that enable developers to write cleaner and more
efficient code. They are widely used with delegates, LINQ, and functional programming approaches.
Mastering lambda expressions can significantly enhance your C# programming skills.
For a deeper dive into lambda expressions, check out this comprehensive guide: Lambda Expressions
in C#.