Just learned the Power of Delegates in C#
Source: Dev.to

🧠 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:
- Create a delegate
- Define its signature
- Pass methods as parameters
- Invoke the delegate
Here’s a visual reference:
⚠️ 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}");
}
}
}
}

📣 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}");
}
}
}
}

📦 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);
}
}


