Delegates and Callback

Ronald Rex 1,666 Reputation points
2023-06-30T20:54:47.6266667+00:00

Hi Friends. I was wondering why do I need a Delegate Callback if I can just call the method the Delegate points to in my Method? I dont understand how using a delegate makes your method more dynamic.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,215 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,601 Reputation points Microsoft Vendor
    2023-07-03T05:46:45.6933333+00:00

    Hi,@Ronald Rex. Welcome Microsoft Q&A. Delegates in C# provide a way to represent a reference to a method, similar to function pointers in other programming languages. They allow you to pass methods as arguments to other methods or store them as variables, making your code more flexible and dynamic. This is particularly useful when you want to decouple the calling code from the implementation of the method, enabling you to change the behavior of a method at runtime or plug in different implementations without modifying the calling code.

    Here is an example to understand the concept:

    Suppose you have a simple calculation method that adds two numbers:

    public int Add(int a, int b)
    {
        return a + b;
    }
    
    

    Now, you want to create a method that performs some additional operations before or after calling the Add method. One way to do this is by using a delegate callback:

    public delegate int CalculationDelegate(int a, int b);
    
    public int PerformCalculation(int a, int b, CalculationDelegate calculationMethod)
    {
        // Additional operations before calling the calculation method
        int result = calculationMethod(a, b);
        // Additional operations after calling the calculation method
        return result;
    }
    

    With this approach, you could pass the Add method as a delegate to PerformCalculation, like this:

    int sum = PerformCalculation(5, 3, Add);
    

    Now, PerformCalculation will execute the Add method and handle any additional operations you might have defined within it. However, you can also pass other calculation methods that follow the same signature as Add, and PerformCalculation will work with them without any modifications.

    For example, let's say you have another calculation method called Multiply:

    public int Multiply(int a, int b)
    {
        return a * b;
    }
    
    

    You could use the same PerformCalculation method to work with Multiply:

    int product = PerformCalculation(5, 3, Multiply);
    

    By using delegates, you can easily switch between different calculation methods without having to modify the PerformCalculation method. This decoupling of the calling code and the implementation provides flexibility and reusability, making your code more dynamic and easier to maintain.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Castorix31 86,496 Reputation points
    2023-07-01T01:27:45.3066667+00:00

    It is explained in MSDN : Using Delegates (C# Programming Guide)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.