Leo
Leo

Reputation: 4657

How to use an event with a delegate

As far as I know the only thing about delegates is that , delegate is:

But something I can't understand is that some users are declaring an event with the eventhandler the delegate. For example:

public void delegate MyDelegate(string Name);
public event MyDelegate EventOfDelegate;

But I don't know what's with the event with the eventhandler of the delegate . Can someone explain to me what's with the event? where I can use it and if the eventhandler is the name of the delegate what it means?

Upvotes: 2

Views: 2402

Answers (3)

BlackBear
BlackBear

Reputation: 22989

public delegate void MyDelegate(string Name);

This line is declaring a delegate with a void return type and a single input parameter of type string. It is then used in the following event declaration, which basically means that the subscribers of this event must have the same signature defined previously in the delegate definition (more or less, read here for more info about the topic).

Upvotes: 0

nikola-miljkovic
nikola-miljkovic

Reputation: 670

you assign some Delegate to EventOfDelegate(As you declared in code). Example EventOfDelegate+= new MyDelegate(arg);

Where arg is action to do when event get's called.

Then when we want to use that event we do

if (EventOfDelegate != null) // check if we assigned it
    EventOfDelegate(arg);

Upvotes: 0

Alberto Solano
Alberto Solano

Reputation: 8227

As you can see in the question posted by @Oded:

An Event declaration adds a layer of abstraction and protection on the delegate instance. This protection prevents clients of the delegate from resetting the delegate and its invocation list and only allows adding or removing targets from the invocation list.

This is needed because, using delegates and events, two roles appear: the broadcaster and the subscriber.

From the "C# 4 in a Nutshell" book:

The broadcaster is a type that contains a delegate field. The broadcaster decides when to broadcast, by invoking a delegate.

The subscribers are the method target recipients. A subscriber decides when to start and stop listening, by calling += and -= on the broadcaster's delegate. A subscriber does not know about, or interfere with, other subscribers.

Then, an event is a construct used to expose the delegate features required for this model, the subscriber/broadcaster model. The main purpose of events is to prevent subscribers from interfering with each other. For example:

Consider a friend and yourself. You sign an agreement with your friend. This agreement consists of:

  • When you have finished doing an activity, your friend has to inform other friends about the end of your activity. Those friends have to do other activities.

In this case, you are the broadcaster, your friend is a subscriber, the end of your activity is the event. What about the delegate? The delegate is your friend, because he has to give the news to other friends about the end of your activity, in order to let them doing other activities.

In other terms:

public delegate void PriceChangedHandler(decimal oldPrice, decimal newPrice);

public class Stock
{
    string symbol;
    decimal price;

    public Stock(string symbol) { this.symbol = symbol; }

    public event PriceChangedHandler PriceChanged;

    public decimal Price
    {
        get {return Price;}
        set
        {
            if(price == value) return;
            if(PriceChanged != null) /* if invocation list is not empty, fire the event */
                PriceChanged(price, value);
            price = value;
        }
    }

You can see that the Stock class fires its PriceChanged event every time the Price of the Stock changes.

Upvotes: 0

Related Questions