Master C# Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Lambda Expressions

Lambda Expressions are a concise way of writing anonymous functions in C#. They are commonly used with delegates, collections, LINQ, filtering, sorting, and event handling.

Simple Definition: A lambda expression is a short way to write a method or function without explicitly declaring a separate method.

What is a Lambda Expression?

A lambda expression allows us to write a small function directly where it is required. Lambda expressions are especially useful when the function is short and does not need a separate method.

Lambda expressions use the => operator, which is commonly read as "goes to".

Lambda Expression Syntax

C#
(parameters) => expression

Simple Lambda Example

C#
using System;
 
Func<int, int> square =
    number => number * number;

Console.WriteLine(square(5));
Output
25

Lambda Expression with Parameters

A lambda expression can accept one or more parameters.

C#
using System;
 
Func<int, int, int> add =
    (a, b) => a + b;

Console.WriteLine(add(10, 20));
Output
30

Lambda Expression without Parameters

A lambda expression can also be created without parameters.

C#
using System;
 
Action message = () =>
{
    Console.WriteLine("WelCome to CIIT Training Institute 👨‍🏫❤️...!");
};

message();
Output
WelCome to CIIT Training Institute 👨‍🏫❤️...!

Lambda Expression with Multiple Statements

When a lambda contains multiple statements, curly braces are used. A return statement can be used when the lambda returns a value.

C#
using System;
 
Func<int, int> calculate = number =>
{
    int result = number * 2;
    return result;
};

Console.WriteLine(calculate(10));
Output
20

Lambda Expression with Action

Action is a built-in delegate used when a lambda expression does not return a value.

C#
using System;
 
Action<string> greet =
    name => Console.WriteLine("Hello " + name);

greet("Rahul");
Output
Hello Rahul

Lambda Expression with Func

Func is a built-in delegate used when a lambda expression returns a value.

C#
using System;
 
Func<int, int> cube =
    number => number * number * number;

Console.WriteLine(cube(3));
Output
27

Lambda with List

Lambda expressions are commonly used with collections such as List.

C#
using System;
 
List<int> numbers =
    new List<int> { 10, 20, 30, 40, 50 };

numbers.ForEach(number =>
{
    Console.WriteLine(number);
});
Output
10
20
30
40
50

Lambda for Filtering Data

Lambda expressions are frequently used to filter collection data.

C#
using System;
 
List<int> numbers =
    new List<int> { 10, 15, 20, 25, 30 };

var result = numbers.Where(number => number > 20);

foreach (int number in result)
{
    Console.WriteLine(number);
}
Output
25
30

Lambda with Find()

The Find() method can use a lambda expression to find the first element that satisfies a condition.

C#
using System;
 
List<int> numbers =
    new List<int> { 10, 20, 30, 40 };

int result = numbers.Find(number => number > 25);

Console.WriteLine(result);
Output
30

Lambda with Sort()

Lambda expressions can also be used to define custom sorting logic.

C#
using System;
 
List<int> numbers =
    new List<int> { 50, 20, 40, 10, 30 };

numbers.Sort((a, b) => a.CompareTo(b));

foreach (int number in numbers)
{
    Console.WriteLine(number);
}
Output
10
20
30
40
50

Lambda Expressions with LINQ

One of the most important uses of lambda expressions is with LINQ. LINQ methods such as Where(), Select(), OrderBy(), and Any() commonly accept lambda expressions.

C#
using System;
 
List<int> marks =
    new List<int> { 45, 70, 85, 90, 35 };

var passedMarks =
    marks.Where(mark => mark >= 50);

foreach (int mark in passedMarks)
{
    Console.WriteLine(mark);
}
Output
70
85
90

Lambda with Objects

Lambda expressions can be used to filter objects based on their property values.

C#
using System;
 
class Student
{
    public string Name { get; set; }
    public int Marks { get; set; }
}

List<Student> students = new List<Student>
{
    new Student { Name = "Samadhan", Marks = 80 },
    new Student { Name = "Omkar", Marks = 45 },
    new Student { Name = "Samiksha", Marks = 90 }
};

var result =
    students.Where(student => student.Marks >= 50);

foreach (Student student in result)
{
    Console.WriteLine(student.Name);
}
Output
Samadhan
Samiksha

Expression Lambda vs Statement Lambda

Type Example
Expression Lambda number => number * 2
Statement Lambda number => { return number * 2; }

Advantages of Lambda Expressions

  • Reduces unnecessary code.
  • Makes short functions easier to read.
  • Works naturally with delegates.
  • Widely used with LINQ.
  • Useful for collection filtering and sorting.
  • Supports functional-style programming.

Common Mistakes

  • Using a lambda where a normal named method would be clearer.
  • Using overly complex lambda expressions.
  • Forgetting the correct parameter types when type inference is not enough.
  • Confusing Action and Func.
  • Writing difficult-to-read nested lambda expressions.

Real-World Example

In a student management application, lambda expressions can be used to find students who have passed an examination.

C#
using System;
 
List<int> marks =
    new List<int> { 35, 55, 72, 40, 88 };

var passed =
    marks.Where(mark => mark >= 50);

foreach (int mark in passed)
{
    Console.WriteLine(mark);
}
Output
55
72
88
CIIT Practical Point: Lambda Expressions are heavily used in modern .NET applications, especially with LINQ, Entity Framework Core, collections, delegates, events, filtering, sorting, and data processing.

Interview Questions

1. What is a Lambda Expression?

A lambda expression is a concise way to represent an anonymous function in C#.

2. Which operator is used in a lambda expression?

The => operator is used in lambda expressions.

3. What is the difference between Action and Func?

Action represents a delegate that returns no value, while Func represents a delegate that returns a value.

4. Where are lambda expressions commonly used?

Lambda expressions are commonly used with LINQ, collections, delegates, events, filtering, sorting, and data processing.

5. Can a lambda expression have multiple statements?

Yes. Multiple statements can be written inside curly braces, and a return statement can be used when a value is required.

Summary

Lambda Expressions provide a concise way to write anonymous functions in C# using the => operator.

You learned how lambda expressions can work with parameters, Action, Func, Lists, filtering, searching, and sorting.

Lambda expressions are especially important when working with LINQ, because LINQ methods commonly use lambda expressions to define filtering and transformation logic.

Understanding Lambda Expressions is an important step toward writing clean, concise, and modern C# and .NET applications.

Practice Questions

  1. Create a lambda expression to calculate the square of a number.
  2. Create a lambda expression to calculate the cube of a number.
  3. Create a lambda expression that adds two numbers.
  4. Create a lambda expression using Action.
  5. Create a lambda expression using Func.
  6. Use a lambda expression with List.ForEach().
  7. Use a lambda expression to filter numbers greater than 50.
  8. Use a lambda expression with Find().
  9. Use a lambda expression with Sort().
  10. Use a lambda expression with LINQ Where().
  11. Use a lambda expression to filter student objects.
  12. Explain the difference between expression and statement lambdas.