Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand the difference between pure delegate and event fields

Delegate : I understand. But when I move to event, many things I don't understand so much. I read book, MSDN and some simple examples on Network, they both have same structures. For example, here is the link : Event Example

I take the first example, that the author said it's the most easiest example about C# Event.

Here is his code :

public class Metronome
{
    public event TickHandler Tick;
    public EventArgs e = null;
    public delegate void TickHandler(Metronome m, EventArgs e);
    public void Start()
    {
        while (true)
        {
            System.Threading.Thread.Sleep(3000);
            if (Tick != null)
            {
                Tick(this, e);
            }
        }
    }
}

public class Listener
{
    public void Subscribe(Metronome m)
    {
        m.Tick += new Metronome.TickHandler(HeardIt);
    }
    private void HeardIt(Metronome m, EventArgs e)
    {
        System.Console.WriteLine("HEARD IT");
    }
}

class Test
{
    static void Main()
    {
        Metronome m = new Metronome();
        Listener l = new Listener();
        l.Subscribe(m);
        m.Start();
    }
}

You can notice line: public event TickHandler Tick. When I change to public TickHandler Tick, program still run the same. But new line I understand because it's just a pure delegate.

So, my question is : what is the real purpose of event keyword in line : public event TickHandler Tick. This is very important, because all examples always use like this, but I cannot explain why.

Thanks :)

like image 615
hqt Avatar asked May 22 '12 16:05

hqt


1 Answers

Delegates and events are related concepts, but they are not the same thing. The term "delegate" tends to have two meanings (often glossed over):

  • A delegate type which is similar to a single method interface. (There are significant differences, but that's a reasonable starting point.)
  • An instance of that type, often created via a method group, such that when the delegate is "invoked", the method is called.

An event is neither of those. It's a kind of member in a type - a pair of add/remove methods, taking a delegate to subscribe to or unsubscribe from the event. The add and remove methods are used when you use foo.SomeEvent += handler; or foo.SomeEvent -= handler;.

This is very similar to how a property is really a pair of get/set methods (or possibly just one of the two).

When you declare a field-like event like this:

public event TickHandler Tick;

the compiler adds members to your class which are somewhat like this:

private TickHandler tick;

public event TickHandler
{
    add { tick += value; }
    remove { tick -= value; }
}

It's a bit more complicated than that, but that's the basic idea - it's a simple implementation of the event, just like an automatically implemented property. From inside the class, you can access the backing field, whereas outside the class you'll always end up just using the event.

Personally I think it's a pity that the declaration of a field-like event looks so much like a field of a delegate type - it leads to some of the misleading (IMO) statements found in some of the answers, as if the event keyword "modifies" a field declaration - when actually it means you're declaring something entirely different. I think it would have been clearer if field-like events looked more like automatically-implemented properties, e.g.

// Not real C#, but I wish it were...
public event TickHandler Tick { add; remove; }

I have a whole article going into rather more detail, which you may find useful.

like image 124
Jon Skeet Avatar answered Nov 05 '22 05:11

Jon Skeet