Just learned the Power of Delegates in C#

Published: (February 25, 2026 at 12:45 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Cover image for Just learned the Power of Delegates in C#

BALIGUAT, JUSTINE JERALD Y.

🧠 What Are Delegates?

Delegates in C# are type‑safe, object‑oriented method pointers.
In simple terms, a delegate lets you pass a method as a parameter to another method. Think of it as storing a reference to a function inside a variable — but in a structured and safe way.

🏗 Creating and Using a Delegate

In the first part of the tutorial I learned how to:

  1. Create a delegate
  2. Define its signature
  3. Pass methods as parameters
  4. Invoke the delegate

Here’s a visual reference:

Delegates Example

⚠️ Important Rule: Parameter Matching

The delegate’s parameter types must match the parameter types of the method being assigned to it.
If the signatures don’t line up, C# throws a compile‑time error – that’s what makes delegates type‑safe.

public delegate void MyDelegate(string message);

public static void ShowMessage(string text)
{
    Console.WriteLine(text);
}

Delegates can also reference instance methods

Below is an example of a delegate referencing an instance method.

using System;
using System.IO;

namespace DelegateBasicExample
{
    delegate void LogText(string text, DateTime datetime);

    class Program
    {
        static void Main(string[] args)
        {
            Logger logger = new Logger();
            LogText logTextToConsole = new LogText(logger.LogTextToConsole);

            Console.WriteLine("Enter some text: ");
            string input = Console.ReadLine();
            logTextToConsole(input, DateTime.Now);
        }
    }

    class Logger
    {
        public void LogTextToConsole(string text, DateTime datetime)
        {
            Console.WriteLine($"{datetime} : {text}");
        }

        public void LogTextToFile(string text, DateTime datetime)
        {
            using (StreamWriter sw = new StreamWriter("log.txt", true))
            {
                sw.WriteLine($"{datetime} : {text}");
            }
        }
    }
}

Delegate instance example

📣 Multi‑cast Delegate

Multiple methods can be assigned to a single delegate instance using the + operator. This lets one delegate invoke several methods sequentially.

using System;
using System.IO;

namespace DelegateBasicExample
{
    delegate void LogText(string text, DateTime datetime);

    class Program
    {
        static void Main(string[] args)
        {
            Logger logger = new Logger();

            LogText logToFile   = new LogText(logger.LogTextToFile);
            LogText logToConsole = new LogText(logger.LogTextToConsole);

            // Combine the two delegates
            LogText multiLog = logToConsole + logToFile;

            Console.WriteLine("Enter some text: ");
            string input = Console.ReadLine();
            multiLog(input, DateTime.Now);
        }
    }

    class Logger
    {
        public void LogTextToConsole(string text, DateTime datetime)
        {
            Console.WriteLine($"{datetime} : {text}");
        }

        public void LogTextToFile(string text, DateTime datetime)
        {
            using (StreamWriter sw = new StreamWriter("log.txt", true))
            {
                sw.WriteLine($"{datetime} : {text}");
            }
        }
    }
}

Multicast delegate example

📦 Passing a Delegate as a Method Argument

A delegate can be passed to another method, which can then invoke it.

using System;
using System.IO;

namespace DelegateBasicExample
{
    delegate void LogText(string text, DateTime datetime);

    class Program
    {
        static void Main(string[] args)
        {
            Logger logger = new Logger();

            LogText logToFile   = new LogText(logger.LogTextToFile);
            LogText logToConsole = new LogText(logger.LogTextToConsole);

            // Pass the delegate to another method
            ProcessLog(logToConsole);
        }

        static void ProcessLog(LogText logAction)
        {
            Console.WriteLine("Enter some text: ");
            string input = Console.ReadLine();
            logAction(input, DateTime.Now);
        }
    }

    class Logger
    {
        public void LogTextToConsole(string text, DateTime datetime)
        {
            Console.WriteLine($"{datetime} : {text}");
        }

        public void LogTextToFile(string text, DateTime datetime)
        {
            using (StreamWriter sw = new StreamWriter("log.txt", true))
            {
                sw.WriteLine($"{datetime} : {text}");
            }
        }
    }
}

That’s it for today’s deep dive into delegates. Happy coding!

.LogTextToConsole);

LogText multiLogText = LogTextToConsole + LogTextToFile;

Console.WriteLine("Enter some text: ");
LogWithDelParam(multiLogText, Console.ReadLine());
}

static void LogWithDelParam(LogText logText, string text)
{
    logText(text, DateTime.Now);
}
}

Image description

0 views
Back to Blog

Related posts

Read more »

[Boost]

Profile !Vincent A. Cicirellohttps://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaw...