codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

26 / 60 topics
17Classes and Objects18Constructors in C#19Inheritance in C#20Polymorphism in C#21Encapsulation in C#22Access Modifiers in C#23Properties in C#24Indexers in C#25Events in C#26Delegates in C#
Tutorials/C# Programming/Delegates in C#
🔷C# Programming

Delegates in C#

Updated 2026-05-15
10 min read

Delegates in C#

Introduction

In the world of programming, especially in languages like C#, delegates play a crucial role. They are reference types that represent references to methods with a specific parameter list and return type. Delegates allow you to pass methods as parameters to other methods, enabling more flexible and dynamic code.

This tutorial will guide you through understanding what delegates are, how to define them, and how to use them effectively in your C# applications. By the end of this section, you'll be able to leverage delegates to create powerful and reusable code.

Concept

What is a Delegate?

A delegate in C# is essentially a type-safe function pointer. It allows you to encapsulate a method reference within an object. Delegates are particularly useful when you need to pass methods as arguments to other methods, or when you want to implement event-driven programming.

Declaring a Delegate

To declare a delegate, you use the delegate keyword followed by the return type and parameter list of the methods it can represent. Here’s how you define a simple delegate:

csharp
1public delegate int Calculate(int x, int y);

In this example, Calculate is a delegate that can reference any method which takes two integers as parameters and returns an integer.

Using Delegates

Once you have defined a delegate, you can create instances of it that point to specific methods. Here’s how you can do it:

csharp
1public class MathOperations
2{
3 public static int Add(int x, int y)
4 {
5 return x + y;
6 }
7
8 public static int Subtract(int x, int y)
9 {
10 return x - y;
11 }
12}
13
14class Program
15{
16 static void Main()
17 {
18 Calculate add = new Calculate(MathOperations.Add);
19 Calculate subtract = new Calculate(MathOperations.Subtract);
20
21 Console.WriteLine("Addition: " + add(10, 5)); // Output: Addition: 15
22 Console.WriteLine("Subtraction: " + subtract(10, 5)); // Output: Subtraction: 5
23 }
24}

In this example, the Calculate delegate is used to reference two methods from the MathOperations class. Instances of the delegate are created and assigned to point to these methods.

Multicast Delegates

Delegates in C# support multicast, meaning a single delegate instance can hold references to multiple methods. When you invoke a multicast delegate, all the methods it points to are executed in order:

csharp
1public class MathOperations
2{
3 public static void PrintAddition(int x, int y)
4 {
5 Console.WriteLine("Addition: " + (x + y));
6 }
7
8 public static void PrintSubtraction(int x, int y)
9 {
10 Console.WriteLine("Subtraction: " + (x - y));
11 }
12}
13
14class Program
15{
16 static void Main()
17 {
18 Calculate operations = new Calculate(MathOperations.PrintAddition);
19 operations += new Calculate(MathOperations.PrintSubtraction);
20
21 operations(10, 5);
22 // Output:
23 // Addition: 15
24 // Subtraction: 5
25 }
26}

In this example, the operations delegate is multicast and points to both PrintAddition and PrintSubtraction. When invoked, both methods are executed.

Examples

Example 1: Basic Delegate Usage

Let’s create a simple application that uses delegates to perform basic arithmetic operations:

csharp
1using System;
2
3public delegate int ArithmeticOperation(int x, int y);
4
5class MathOperations
6{
7 public static int Add(int x, int y)
8 {
9 return x + y;
10 }
11
12 public static int Subtract(int x, int y)
13 {
14 return x - y;
15 }
16}
17
18class Program
19{
20 static void Main()
21 {
22 ArithmeticOperation add = new ArithmeticOperation(MathOperations.Add);
23 ArithmeticOperation subtract = new ArithmeticOperation(MathOperations.Subtract);
24
25 Console.WriteLine("Addition: " + add(10, 5)); // Output: Addition: 15
26 Console.WriteLine("Subtraction: " + subtract(10, 5)); // Output: Subtraction: 5
27 }
28}

Example 2: Multicast Delegates

Here’s an example that demonstrates multicast delegates:

csharp
1using System;
2
3public delegate void PrintOperation(int x, int y);
4
5class MathOperations
6{
7 public static void PrintAddition(int x, int y)
8 {
9 Console.WriteLine("Addition: " + (x + y));
10 }
11
12 public static void PrintSubtraction(int x, int y)
13 {
14 Console.WriteLine("Subtraction: " + (x - y));
15 }
16}
17
18class Program
19{
20 static void Main()
21 {
22 PrintOperation operations = new PrintOperation(MathOperations.PrintAddition);
23 operations += new PrintOperation(MathOperations.PrintSubtraction);
24
25 operations(10, 5);
26 // Output:
27 // Addition: 15
28 // Subtraction: 5
29 }
30}

What's Next?

In the next section, we will explore exceptions in C#. Understanding how to handle errors and exceptions is crucial for building robust applications. Make sure to check out our tutorial on "Exceptions in C#" to learn more about try-catch blocks, custom exceptions, and exception handling best practices.

Happy coding!


PreviousEvents in C#Next Exceptions in C#

Recommended Gear

Events in C#Exceptions in C#