C# Delegates

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

 

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

 

In c#, delegate is a reference type and it’s a 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 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>(<parameters>)

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

 

public delegate void UserDetails(string name);

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

 

The above defined method “UserDetails” can be pointed to any other method which is having same parameters and return type.

 

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

C# Delegate Example

Following is the example of declaring and using a delegate 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);

        }

    }

    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 above example, we created a delegate called “SampleDelegate” and the delegate has been instantiated by using defined methods.

 

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

 

C# Delegate Example Result

 

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

 

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

 

SampleDelegate dlgt = m.Add;

dlgt(10, 90);

// or

dlgt.Invoke(10, 90);

C# Delegate Types

In c#, we have a two different type of delegates available, those are

 

  • Single cast Delegates
  • Multicast Delegates

C# Single Cast Delegates

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

C# Multicast Delegates

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

 

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

 

In c#, Multicast delegates will work with only the methods which is having a void as return type. In case, if we want to create a multicast delegate with return type, then we will get a return type of 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 above example, we created a delegate called “SampleDelegate” and holding the reference of multiple methods using "+" operator. Now, our delegate become a multicast delegate and invoking a dlgt instance will invoke all the methods sequentially.

 

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

 

C# Multicast Delegate Example Result

 

This is how we can use multicast delegates to hold a reference of multiple methods based on our requirements.

C# Pass Method as Parameter using Delegate

In c#, by using delegate we can pass a methods as parameter. Following is the example of sending a methods as parameter using 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 above example, we created a “SampleMethod” with a delegate as parameter type. By declaring like this, we can pass method as a parameter to newly created method (SampleMethod).

 

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

 

C# Send a Method as Parameter using Delegate Example Result

C# Delegates Overview

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

 

  • We need to use a delegate keyword to define a delegates.
  • In c#, delegates are used to hold the reference of one or more methods which is having same signature as delegate.
  • In c#, delegates are like function pointers in C++ but these are type safe and secure.
  • By using delegates, we can pass a methods as 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 a multiple methods to delegates.
  • By using delegates, we can call a multiple methods with single event.
 

PREVIOUS

C# Interface
 

NEXT

C# Events