Delegates in C#

In c#, the delegate is a type that defines a method signature, and it is useful to hold the reference of one or more methods which are having the same signatures.

 

By using delegates, you can invoke the methods and send methods as an argument to other methods.

 

In c#, the delegate is a reference type, and it’s type-safe and secure. The delegates are similar to function pointers in c++.

C# Delegate Declaration Syntax

In c#, the declaration of delegate will be same as method signature, but the only difference is we will use a delegate keyword to define delegates.

 

Following is the syntax of defining a delegate using delegate keyword in c# programming language.

 

<access_modifier> delegate <return_type> <delegate_name>()

Following is the example of declaring a delegate using delegate keyword in c#.

 

public delegate void UserDetails(string name);

If you observe the above example, a delegate's declaration is same as a method declaration with required parameter types and return value.

 

The above defined method “UserDetails” can be pointed to any other method with the same parameters and return type.

 

In c#, the delegates must be instantiated with a method or expression with the same return type and parameters. We can invoke a method through the delegate instance.

C# Delegate Example

Following is the example of creating delegates in the c# programming language.

 

using System;

namespace Tutlane
{
    // Declare Delegate
    public delegate void SampleDelegate(int a, int b);
    class MathOperations
    {
        public void Add(int a, int b)
        {
           Console.WriteLine("Add Result: {0}", a + b);
        }
        public void Subtract(int x, int y)
        {
           Console.WriteLine("Subtract Result: {0}", x - y);
        }
    }
    class Program
    {
       static void Main(string[] args)
       {
          Console.WriteLine("****Delegate Example****");
          MathOperations m = new MathOperations();
          // Instantiate delegate with add method
          SampleDelegate dlgt = m.Add;
          dlgt(10, 90);
          // Instantiate delegate with subtract method
          dlgt = m.Subtract;
          dlgt(10, 90);
          Console.ReadLine();
       }
    }
}

If you observe the above example, we created a delegate called “SampleDelegate” and the delegate has been instantiated by using defined methods.

 

When you execute the above c# program, you will get the result as shown below.

 

C# Delegate Example Result

 

This is how we can use delegates in our applications to call all the methods which are having the same signatures with a single object.

 

In c#, you can invoke the delegates same as method or by using the Invoke method like as shown below.

 

SampleDelegate dlgt = m.Add;
dlgt(10, 90);
// or
dlgt.Invoke(10, 90);

Types of Delegates in C#

In c#, we have two different types of delegates available. Those are

 

  • Single cast Delegates
  • Multicast Delegates

Single Cast Delegates in C#

In c#, a delegate that points to a single method is called a single cast delegate, and it is used to hold the reference of a single method as explained in the above example.

Multicast Delegates in C#

In c#, a delegate that points to multiple methods is called a multicast delegate, and it is used to hold the reference of multiple methods with a single delegate.

 

By using "+" operator, we can add the multiple method references to the delegate object. Same way, by using "-" operator we can remove the method references from the delegate object.

 

In c#, Multicast delegates will work with only the methods that are having void as return type. If we want to create a multicast delegate with the return type, then we will get a return type of the last method in the invoking list.

C# Multicast Delegate Example

Following is the example of implementing a multicast delegate to hold the reference of multiple methods with "+" operator in c# programming language.

 

using System;

namespace Tutlane
{
    // Declare Delegate
    public delegate void SampleDelegate(int a, int b);
    class MathOperations
    {
       public void Add(int a, int b)
       {
          Console.WriteLine("Add Result: {0}", a + b);
       }
       public void Subtract(int x, int y)
       {
          Console.WriteLine("Subtract Result: {0}", x - y);
       }
       public void Multiply(int x, int y)
       {
          Console.WriteLine("Multiply Result: {0}", x * y);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
           Console.WriteLine("****Delegate Example****");
           MathOperations m = new MathOperations();
           // Instantiate delegate with add method
           SampleDelegate dlgt = m.Add;
           dlgt += m.Subtract;
           dlgt += m.Multiply;
           dlgt(10, 90);
           Console.ReadLine();
       }
    }
}

If you observe the above example, we created a delegate called “SampleDelegate” and holding the reference of multiple methods using "+" operator. Our delegate becomes a multicast delegate, and invoking a dlgt instance will invoke all the methods sequentially.

 

When you execute the above c# program, you will get the result as shown below.

 

C# Multicast Delegate Example Result

 

This is how we can use multicast delegates in c# to hold the reference of multiple methods based on our requirements.

Pass Method as Parameter using Delegate

By using delegates in c#, we can pass methods as the parameter. Following is the example of sending methods as a parameter using a delegate.

 

using System;

namespace Tutlane
{
    // Declare Delegate
    public delegate void SampleDelegate(int a, int b);
    class MathOperations
    {
       public void Add(int a, int b)
       {
          Console.WriteLine("Add Result: {0}", a + b);
       }
       public void Subtract(int x, int y)
       {
          Console.WriteLine("Subtract Result: {0}", x - y);
       }
       public void Multiply(int x, int y)
       {
          Console.WriteLine("Multiply Result: {0}", x * y);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
           Console.WriteLine("****Delegate Example****");
           MathOperations m = new MathOperations();
           SampleMethod(m.Add, 10, 90);
           SampleMethod(m.Subtract, 10, 90);
           SampleMethod(m.Multiply, 10, 90);
           Console.ReadLine();
       }
       static void SampleMethod(SampleDelegate dlgt, int a, int b)
       {
           dlgt(a, b);
       }
    }
}

If you observe the above example, we created a “SampleMethod” with a delegate as a parameter type. By declaring like this, you can pass the method as a parameter to the newly created method (SampleMethod).

 

When you execute the above c# program, you will get the result as shown below.

 

C# Send a Method as Parameter using Delegate Example Result

C# Delegates Overview

The following are the important properties of delegate in c# programming language.

 

  • We need to use a delegate keyword to define delegates.
  • In c#, delegates are used to hold the reference of one or more methods that have the same signature as delegates.
  • In c#, delegates are like function pointers in C++, but these are type-safe and secure.
  • By using delegates, you can pass methods as a parameter to the other methods.
  • In c#, we can invoke delegates as normal methods or by using Invoke property.
  • By using "+" operator, we can add multiple methods to delegates.
  • By using delegates, we can call multiple methods with a single event.