C# Delegate Tutorial with Easy Example

Events and Delegates
In this tutorial you will learn:

  • What is Delegates?
  • How to declare, initialize and invoke Delegates?
  • Easy Programming Example

This article explains how to create and manipulate Delegates in c#. Before starting delegates you must understand what delegate is?

What is Delegate?

You may get numbers of complex reasons of using delegates on different site. I am not going to put complex paragraph here. In simple word delegates contains reference of several methods and call them when needed. So, you create numbers of methods as you need and attach it with delegates. At runtime an event gets fired and delegates dynamically call the function and shows result.

Real World Example

Let me draw a picture for you. For example what will you do when you are in a railway station and suddenly hear a fire alarm. What would you do? You will run immediately outside of the campus. So, let me put this scenario in delegates.

  1. When you hear fire alarm it is actually an event. As you hear fire alarm you initiate to run() function to escape.
  2. But if you hear announcement about your train you don’t escape and go to suitable platform to catch the train.
  3. If you hear your name in announcement that someone is on phone-line and want to talk to you; you go to enquiry counter to attend the phone.
  4. If you hear that train is 6 hour late you may think for a movie meanwhile the time.

So, in real world example we also react differently based on event or situation. In programming, delegate does the same thing. We create numbers of suitable function and execute them based on Event.

fig-delegate




  • Some facts about delegates

    1. Delegates are reference type but instead of referencing objects it reference methods.
    2. Delegates have no method body.
    3. Delegates are type-safe, object-oriented and secure.
    4. A Delegate is a function pointer that allows you to reference a method.
    5. Delegates encapsulate methods.
    6. A Function that is added to delegates must have same return type and same signature as delegate.
    7. A Delegate can be created using delegate keyword.
      1. Delegates with no parameter and no return type

      2. Delegates with parameter but no return type

        Or,

      3. Delegates with parameters and return type

    8. A delegate has 3 steps
      1. Declaration
      2. Instantiation
      3. Invocation

    Programming Example

    Here, I am showing you very basic delegates programming. Once you understand how delegates work you will be able to crack complex delegates codes.

    Output

    I am Function 1
     
    I am Function 2
     
    I am Function 3
    _

    Explanation

    1. In this programming Example, I have created a delegate

    2. I have also created 3 functions

    3. In the main function creates object of delegates. You must remember that a delegate always require a function while initialized.

    4. Finally call the delegates to execute the function.

    Example 2

    Output

    Addition of 5 and 10 is : 15

    Subtraction of 5 and 10 is : -5

    Summary

    In this chapter, you learned what is delegates and how to use it in programming. In the next chapter, you will learn about multicast delegates with example.


    One thought on “C# Delegate Tutorial with Easy Example”

    Leave a Reply

    Your email address will not be published. Required fields are marked *