Understanding Delegates in C#

Tutorial 1 of 5

Understanding Delegates in C

1. Introduction

In this tutorial, we will explore delegates in C#, an essential feature that brings flexibility and modularity to your programs. By the end of this tutorial, you will understand what delegates are, how to declare and use them, and where they are applicable.

Prerequisites: Basic knowledge of C# programming. Familiarity with concepts like classes, methods, and events is beneficial.

2. Step-by-Step Guide

Delegates in C# are similar to function pointers in C or C++, but they are type-safe. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.

Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.

Declaring Delegates

To declare a delegate, you use the delegate keyword, followed by a function signature, and finally a delegate name. Here's a simple example:

public delegate int MyDelegate(string s);

In this example, MyDelegate is a delegate type that can encapsulate a method taking a string as a parameter and returning an int.

Using Delegates

Once a delegate type is declared, a delegate object can be created with the new keyword and be associated with a particular method. Here's an example:

public int StringLength(string s) 
{
    return s.Length;
}

MyDelegate del = new MyDelegate(StringLength);

In this example, the del delegate object is associated with the StringLength method. This means that invoking del will invoke StringLength.

3. Code Examples

Basic Delegate Usage

Below is a full example of declaring, instantiating, and using a delegate.

using System;

public delegate int MyDelegate(string s); // Declare a delegate

class Program
{
    public static int ShowStringLength(string s) // Method that matches delegate signature
    {
        return s.Length;
    }

    static void Main()
    {
        MyDelegate del = new MyDelegate(ShowStringLength); // Instantiate the delegate

        int result = del("Hello, world!"); // Invoke the delegate

        Console.WriteLine(result); // Outputs: 13
    }
}

The expected output of this program is 13, which is the length of the string "Hello, world!".

4. Summary

In this tutorial, we've learned about delegates in C#, including their declaration, instantiation, and usage. Delegates are a powerful tool in C#, allowing for runtime method invocation and serving as the foundation for events.

To continue learning about delegates, look into multicast delegates and generic delegates, which are more advanced topics. The official Microsoft documentation is a great place to start.

5. Practice Exercises

  1. Create a delegate CalculateDelegate that can encapsulate methods taking two integers and returning an integer. Use this delegate to create methods that implement addition, subtraction, and multiplication.
  2. Modify the above program to use anonymous methods with the delegate.

Solutions will be provided in the comments.