Skip to content Skip to footer

C# Delegates

Delegates are a powerful feature in programming that enables to encapsulate methods and reference them in a type-safe manner. They are pointers to methods that can be assigned and passed around like any other variable. Delegates provide a flexible way to implement callback functions, event handlers, and other similar mechanisms in , making writing modular and reusable code easier. By using delegates, can build highly adaptive and extensible systems.

A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, however, delegates are object-oriented, type-safe, and secure. The delegate type is defined by its name and can be used to represent a method as an object.

To declare a delegate, you use the delegate keyword. For example, to create a delegate named Callback that can encapsulate a method taking a string argument and returning void, you can use the following syntax:

public delegate void Callback(string message);

You can create a delegate by providing the name of the method it will wrap or using a lambda expression. For example:

// Create a method for the delegate.
public static void DelegateMethod(string message)

// Instantiate the delegate.
Callback handler = DelegateMethod;

// Call the delegate.
handler("Hello World");

In this example, the handler is a delegate that points to the delegate method. When the handler is called, it executes the DelegateMethod and prints “Hello World” to the console.

Delegates can be passed as parameters to methods, allowing for asynchronous callbacks or custom behavior. For example:

public static void MethodWithCallback(int param1, int param2, Callback callback)
    callback("The number is: " + (param1 + param2));

// Usage:
MethodWithCallback(1, 2, handler);
// Output: "The number is: 3"

Here, MethodWithCallback accepts a delegate (callback) and invokes it later. The delegate can be any method that matches the signature of Callback, allowing for great flexibility and reusability.

You can associate a delegate with a named method or use an anonymous method. For example:

// Declare a delegate.
delegate void WorkCallback(int x);

// Define a named method.
void DoWork(int k)
    // ...

// Instantiate the delegate using the named method.
WorkCallback workHandler = DoWork;

Anonymous methods can also be used as delegate targets.

Delegates are a robust and flexible feature in that allows to build more modular and reusable code. By encapsulating methods and referencing them in a type-safe manner, delegates provide a powerful way to handle events, callbacks, and dynamic method invocation.

Leave a comment

Newsletter Signup

The Grid —
The Matrix Has Me
Big Bear Lake, CA 92315

01010011 01111001 01110011 01110100 01100101 01101101 00100000
01000110 01100001 01101001 01101100 01110101 01110010 01100101

You're in trouble, program. Why don't you make it easy on yourself. Who's your user?Master Control Program

Deitasoft © 2024. All Rights Reserved.