Rajanikant Hawaldar
Can you explain me how delegate and events works?

Can you explain me how delegate and events works?

By Rajanikant Hawaldar in C# on Jul 14 2021
  • Sonil  Kumar
    Jul, 2021 25

    Delegates are funciton pointer. Which hold the address of a function. And execute it when caller need that. It can hold multiple function in a single delegate. But the Function signature should match with the delegate signature.
    Events are the action to call these delegates. Whenever any event raised it will associated with a delegate and call that delegate and delegate the execute all the function in the sequence in which they were added to the delegate.

    //This is a public delegate which can hold any function which has a void return and taking a single integer type parameter.
    public delegate void CustomDelegate(int x);

    //This is an event which is linked with CustomDelegate. When user will call this event, it will pass that signal with data to the delegate and that delegate further execute the method available.
    event CustomDelegate myCustomEvent;

    • 2
  • Rajeev Kumar
    Mar, 2023 17

    Delegates hold a reference to a method, and (for instance methods) a reference to the target object the method should be called on. Delegates types are declared with the delegate keyword. They can appear either on their own or nested within a class, as shown below. This code declares two delegate types.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS