Delegates in C#

C# Programming Examples - Delegates

Delegates in C# are like special variables that can hold references to methods or objects that point to methods. They work similarly to function pointers in C/C++ and let us choose which method to call when an event happens. Delegates are part of the System namespace and are often used for implementing callback methods and events. Additionally, they can also call anonymous methods and lambda expressions.

There are three stages to follow while working with delegates:

  • Declaration of Delegates
  • Instantiation of Delegates
  • Invocation of Delegates

Delegate Syntax:

Declaration of a delegate is done by using the delegate keyword followed by a function signature, as indicated below.

[access modifier] delegate [return type] [delegate name]([parameter list]);

Types of Delegates:

advertisement
advertisement

In C#, there are three different types of delegates.

  • Singlecast Delegate
  • Multicast Delegate
  • Generic Delegate

Singlecast Delegate

Single Cast Delegate is a delegate type that can only refer to one method at a time. It derives from the System.Delegate class.

Multicast Delegate

The multicast delegate can be used to call multiple methods at the same time. The “+” operator adds a function to the call list, and the “-” operators remove it.

Generic Delegate

The delegate instance does not need to be defined to use the Generic Delegates methods. There are three types of generic delegates: Func, Action, and Predicate.

C# Programming Examples on Delegates

The following section contains C# programs on delegates, delegate functions, delegate types, and math operations using a delegate. Every example program includes the problem description, problem solution, C# code, program explanation, and run-time test cases. All C# examples have been compiled and tested on Visual Studio.

Here is the listing of C# programming examples on Delegates:

advertisement
  1. C# Programs on Delegate Functions
  2. C# Programs on Delegate Types
  3. C# Programs on Math Operations using Delegate

1. C# Programs on Delegate Functions

Program Description
Combine Two Delegates in C# C# Program to Combine Two Delegates
Array of Delegates in C# C# Program to Illustrate Array of Delegates
Display Results using Delegate in C# C# Program to Display Results using Delegates

2. C# Programs on Delegate Types

Program Description
Delegates in C# C# Program to Implement Delegates
Multicast Delegates in C# C# Program to Implement Multicast Delegates
Generic Delegate in C# C# Program to Create Generic Delegate
Declare and Instantiate Delegates in C# C# Program to Declare and Instantiate Delegates

3. C# Programs on Math Operations using Delegates

Program Description
Call Math Operations using Delegates in C# C# Program to Call Math Operations using Delegates
Arithmetic Operations using Delegates in C# C# Program to Implement Arithmetic Operations using Delegates
Convert Feet to Inches using Delegates in C# C# Program to Convert Feet to Inches using Delegates
Delegate Principles in C# C# Program to Implement Principles of Delegates
Call 2 Methods using Delegates in C# C# Program to Use Delegate to Call 2 Methods within which First method Prints to Console and Second Method Prints to File

Sample Delegate Program using C#:

Example 1:

/*
 *  C# Program to Display Results using Delegates
 */
using System;
public class example
{
    public delegate int DelegateHandler(int a, int b);
    public static void Main(string[] args)
    {
        Results Results = new Results();
        DelegateHandler sum = new DelegateHandler(Results.sum);
        int result = sum(50, 20);
        Console.WriteLine("Result is: " + result);
        Console.ReadLine();
    }
}
 
public class Results
{
    public int sum(int a, int b)
    {
        return a + b;
    }
}

Program Output:

advertisement
Result is: 70

Example 2: Multicast Delegates in C#

This C# program is used to implement multicast delegates. Using Add and Sub two methods perform addition and subtraction.

/*
 * C# Program to Implement Multicast Delegates
 */
 
using System;
delegate void dele(int a, int b);
public class Oper
{
    public static void Add(int a, int b)
    {
        Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
    }
 
   public static void Sub(int a, int b)
    {
        Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
    }
}
public class program
{
    static void Main()
    {
        dele del = new dele(Oper.Add);
        del += new dele(Oper.Sub);
        del(4, 2);
        del -= new dele(Oper.Sub);
        del(1, 9);
        Console.Read();
    }
}
Program Output
 
4 + 2 = 6
4 - 2 = 2
1 + 9 = 10

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.