Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Lambda Expressions in C#
A lambda expression in C# is an anonymous function that provides a concise way to write inline functions. Lambda expressions use the => operator, read as "goes to", which separates the input parameters from the expression body.
Lambda expressions are commonly used with LINQ methods, event handlers, and delegates to create short, readable code without defining separate methods.
Syntax
Following is the basic syntax for lambda expressions −
(input-parameters) => expression
For single parameter, parentheses are optional −
x => x * 2
For multiple statements, use curly braces −
(x, y) => {
int sum = x + y;
return sum * 2;
}
Using Lambda with List Methods
Example
Here we find the first occurrence of an element greater than 50 from a list −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> list = new List<int> { 44, 6, 34, 23, 78 };
int res = list.FindIndex(x => x > 50);
Console.WriteLine("Index: " + res);
// Find the actual element
int element = list.Find(x => x > 50);
Console.WriteLine("Element: " + element);
}
}
The output of the above code is −
Index: 4 Element: 78
Using Lambda with LINQ
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Filter even numbers
var evenNumbers = numbers.Where(x => x % 2 == 0).ToList();
Console.WriteLine("Even numbers: " + string.Join(", ", evenNumbers));
// Square each number
var squares = numbers.Select(x => x * x).ToList();
Console.WriteLine("Squares: " + string.Join(", ", squares));
// Sum of numbers greater than 5
int sum = numbers.Where(x => x > 5).Sum();
Console.WriteLine("Sum of numbers > 5: " + sum);
}
}
The output of the above code is −
Even numbers: 2, 4, 6, 8, 10 Squares: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 Sum of numbers > 5: 30
Lambda with Multiple Parameters
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> words = new List<string> { "apple", "banana", "cherry", "date" };
// Sort by length, then alphabetically
var sorted = words.OrderBy(w => w.Length).ThenBy(w => w).ToList();
Console.WriteLine("Sorted: " + string.Join(", ", sorted));
// Custom comparison using multiple parameters
Func<int, int, int> add = (x, y) => x + y;
Console.WriteLine("5 + 3 = " + add(5, 3));
Func<int, int, bool> isGreater = (x, y) => x > y;
Console.WriteLine("10 > 7: " + isGreater(10, 7));
}
}
The output of the above code is −
Sorted: date, apple, banana, cherry 5 + 3 = 8 10 > 7: True
Lambda vs Anonymous Methods
| Lambda Expression | Anonymous Method |
|---|---|
More concise syntax using =>
|
Uses delegate keyword |
x => x * 2 |
delegate(int x) { return x * 2; } |
| Can be converted to expression trees | Cannot be converted to expression trees |
| Preferred for LINQ and modern C# code | Legacy approach, less commonly used |
Conclusion
Lambda expressions in C# provide a concise way to create anonymous functions using the => operator. They are essential for LINQ operations, functional programming patterns, and creating clean, readable code when working with collections and delegates.
