Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# Lambda Expressions


Share

Lambda Expressions

A lambda expression is a short method without a name. You write it directly where you need it, often in one line.

Here is a normal method that multiplies a number by 2, and the same thing written as a lambda expression:

Example

// A normal method
static int MultiplyByTwo(int x)
{
  return x * 2;
}

// The same as a lambda expression
x => x * 2

Read x => x * 2 as: "take x, and return x * 2".

  • The parameter is on the left side of the arrow =>
  • The value to return is on the right side

You don't need a name, a type, the return keyword or curly braces. A lambda is useful when you need a small piece of code in one place only.


Store a Lambda in a Variable

To store a lambda in a variable, use the type Func (short for function). Inside the < >, write the type of the parameter, and then the type of the return value:

Example

Func<int, int> multiplyByTwo = x => x * 2;
Console.WriteLine(multiplyByTwo(5));  // 10
Console.WriteLine(multiplyByTwo(8));  // 16

Try it Yourself »

Func<int, int> means: the lambda takes an int and returns an int. You call the variable like a method: multiplyByTwo(5).

A lambda can have more than one parameter. Put the parameters inside parentheses. In Func, the last type is always the type of the return value:

Example

Func<int, int, int> add = (a, b) => a + b;
Console.WriteLine(add(3, 4));  // 7

Try it Yourself »



Lambdas Without a Return Value

If the lambda does something, but does not return a value (like a void method), use Action instead of Func. Inside the < >, write the parameter types:

Example

Action<string> greet = name => Console.WriteLine("Hello " + name);
greet("Liam");
greet("Jenny");

Try it Yourself »

Here is a summary of the types used on this page:

TypeThe lambda takesThe lambda returns
Func<int, int>one intan int
Func<int, int, int>two int valuesan int
Action<string>one stringnothing

Lambdas With Lists

Lambdas are often sent to other methods. Many List methods take a lambda that says what to do with each item.

FindAll() runs the lambda for each item. It returns a new list with all the items where the lambda returns true:

Example

List<int> scores = new List<int> { 8, 15, 3, 22, 11 };
List<int> highScores = scores.FindAll(s => s > 10);

foreach (int score in highScores)
{
  Console.WriteLine(score);
}

Try it Yourself »

The lambda s => s > 10 takes a score s, and returns true if the score is greater than 10. The parameter name is up to you. Here, s is short for score.

ForEach() runs the lambda once for each item in the list. It is a short way to write a foreach loop:

Example

List<string> cars = new List<string> { "Volvo", "BMW", "Ford" };
cars.ForEach(car => Console.WriteLine(car));

Try it Yourself »

In the next chapter, you will use lambdas with LINQ, to filter, sort and count the items in a list.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.