Skip to content
dreamcode
dreamcode
Map
Delegates & lambdas
Lesson 16 of 21
+15 XP on finish
C# ADVANCEDChapter 5 · C# Advanced

Delegates and Lambdas

A delegate is a type that represents references to methods. Modern C# uses pre-defined delegates like Action (no return) and Func (returns a value) paired with lambdas.

Worked example
using System;

class Program {
    static void Main() {
        Func<int, int> square = x => x * x;
        Console.WriteLine(square(5));
    }
}

How it reads

  • Func<int, int> is a delegate taking an int and returning an int
  • **x => x * x** is a lambda expression performing the math operation
Cloud tip: Use Action for methods that return void (no value), and Func for methods that return a value.

Check your understanding

0 / 2

Answer all 2 to complete this lesson and earn 15 XP.

  1. 1. Which delegate should you use for a method that does NOT return a value?
  2. 2. What does the operator => represent in C#?
Answer every question to unlock the next lesson.