Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to use events and delegates?

Tags:

c#

delegates

I used events and delegates in many of my projects,but still i am having some doubts in my mind where to use the events and delegates in a project and what is the difference between a delegate and an event.Can anyone explain it please?

like image 364
karthikeyan Avatar asked Dec 02 '10 12:12

karthikeyan


People also ask

Where delegates are used?

Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.

How are delegates used in events?

A delegate is a way of telling C# which method to call when an event is triggered. For example, if you click a Button on a form, the program would call a specific method. It is this pointer that is a delegate. Delegates are good, as you can notify several methods that an event has occurred, if you wish so.

What is the use of delegates and events in C#?

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

Where have you used delegates in C#?

Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class. There are three steps in using delegates. These include declaration, instantiation, and invocation.


2 Answers

A Delegate is a type that can encapsulate a method call. You should use a delegate when you want to treat the method as an object and pass it around.

An Event is just a way of exposing a delegate as a property to which any code outside your class can attach their handlers but cannot invoke the delegate. If you expose your delegate as a public property, code outside your class can not only attach their handlers but also invoke the delegate.

In addition Events also allow you to add remove methods which are called when the handlers are attached\detached to it, just like the getter and setter of a public property allowing you to control the process better.

You should use event when you want other classes to subscribe to an event in your class and be notified when it happens.

public delegate void MyMessageHandlerType(string message);

public class EventTest
{
    public event MyMessageHandlerType MessageEvent
    {
        add { } // invoked when MessageEvent += SomeMethod
        remove { } // invoked when MessageEvent -= SomeMethod
    }
}
like image 95
Unmesh Kondolikar Avatar answered Sep 24 '22 20:09

Unmesh Kondolikar


Both delegates and events provide a callback mechanism. An event provides a callback mechanism that is somewhat less connected than a delagate, in that it may have any number of attached handlers, and that the code often works the same way regardless of the number of handlers (if any). A good example could be a Resized event of a window; the window resizing will work whether or not there are any event handler connected; the event is a notification mechanism.

A delegate is more connected in many senses. Typically you can associate exactly one method with a delegate member, and often they are expected; take the LINQ extension methods (such as Where) for instance: they will not work unless you provide exactly one method for the delegate parameter.

like image 25
Fredrik Mörk Avatar answered Sep 24 '22 20:09

Fredrik Mörk