Lambda expressions in C# provide a concise way to represent anonymous methods. They are used to create inline functions that can be passed as arguments or used in LINQ queries. The lambda operator (=>) separates the input parameters (left side) from the expression or statements (right side).
Types of Lambda Expressions
1. Expression Lambda
Represents a single expression that returns a value.
Syntax:
input => expression;
2. Statement Lambda
Represents a block of statements enclosed in braces.
Syntax:
input => { statements };
Example 1: Lambda Expressions with Collections
In the below example, a list of integers is processed using lambda expressions to calculate the square of each element and filter numbers divisible by 3.
C#
// C# program to demonstrate Lambda Expressions
using System;
using System.Collections.Generic;
using System.Linq;
namespace LambdaExpressions
{
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int>() { 36, 71, 12, 15, 29, 18, 27, 17, 9, 34 };
Console.Write("The list: ");
foreach (var value in numbers)
Console.Write($"{value} ");
Console.WriteLine();
// Lambda to calculate square of each number
var squares = numbers.Select(x => x * x);
Console.Write("Squares: ");
foreach (var value in squares)
Console.Write($"{value} ");
Console.WriteLine();
// Lambda to find numbers divisible by 3
var divisibleBy3 = numbers.FindAll(x => x % 3 == 0);
Console.Write("Numbers Divisible by 3: ");
foreach (var value in divisibleBy3)
Console.Write($"{value} ");
Console.WriteLine();
}
}
}
OutputThe list: 36 71 12 15 29 18 27 17 9 34
Squares: 1296 5041 144 225 841 324 729 289 81 1156
Numbers Divisible by 3: 36 12 15 18 27 9
Example 2: Using Lambda Expressions with User-defined Classes
Lambda expressions can also be used with custom objects. The following example sorts a list of students by name using a lambda expression.
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Student
{
public int RollNo { get; set; }
public string Name { get; set; }
}
class GFG
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student { RollNo = 1, Name = "Liza" },
new Student { RollNo = 2, Name = "Stewart" },
new Student { RollNo = 3, Name = "Tina" },
new Student { RollNo = 4, Name = "Stefani" },
new Student { RollNo = 5, Name = "Trish" }
};
// Sorting by Name using lambda
var sortedList = students.OrderBy(x => x.Name);
foreach (var s in sortedList)
Console.WriteLine($"{s.RollNo} {s.Name}");
}
}
Output1 Liza
4 Stefani
2 Stewart
3 Tina
5 Trish
What is the lambda operator used in C# lambda expressions?
Explanation:
The => operator separates input parameters from the expression in a lambda.
Which syntax correctly defines a statement lambda in C#?
Explanation:
Statement lambdas use braces and can include multiple statements.
How do you sort a list of objects by a property using a lambda expression?
-
list.OrderBy(x => x.PropertyName);
-
list.Sort(x => x.PropertyName);
-
list.Filter(x => x.PropertyName);
-
list.Select(x => x.PropertyName);
Explanation:
OrderBy with a lambda selects the property to sort by.
Quiz Completed Successfully
Your Score : 2/3
Accuracy : 0%
Login to View Explanation
1/3
1/3
< Previous
Next >
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers