Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have been studying events in C# because I want to create more modular programs where my classes don't have to be aware of my forms. The idea is that my forms will be updated via events raised by instances of my classes.

Having studied this via a few different books I think I'm getting to grips with the concept in general. I can certainly create classes that can raise events, and I can subscribe to those events and take appropriate actions. However, I remain confused about the role of the delegate in the events process:

C#
delegate void MyEventHandler();

class Test
{
    public event MyEventHandler Results;

    public void OnResults()
    {
        if (Results != null)
            Results();
    }
}

namespace EventsExperiments
{
    class Program
    {
        static void Handler()
        {
            Console.WriteLine("Event received by program object");
            Console.ReadKey();
        }

        static void Main(string[] args)
        {
            Test event1 = new Test();
            event1.Results += Handler;

            event1.OnResults();
        }
    }
}


I understand that in the above short excerpt I have created a delegate called MyEventHandler() which is going to act as a reference (like a pointer) for the events. Then I have created a class called Test and I have given it the ability to raise events that the delegate will accept.

MY QUESTION:

1.
With regard to the delegate, is it correct to say that each time an event is raised, this delegate will point to the handler method of the object that subscribes? And, if there are multiple objects subscribing to the event, then the delegate will point to each one of their subscribed handler methods in turn?
If this understanding is correct, then where do I see the reassignment of the delegate in my code?

Perhaps I am fundamentally misunderstanding something, and in that case I hope someone can set me straight.

Cheers!

Brian
Posted

1 solution

You are correct in your assumptions an event can be assigned multiple delegates.

A delegate is a description of a method signature.

When you instantiate a delegate you have to point it to a specific method which matches the description of the delegate.

Visual studio allows developers to be quite lazy. The line where you assign the event:

C#
event1.Results += Handler


In reality what is happening is this:

C#
event1.Results += new MyEventHandler(Handler)


You create an instance of the delegate, pointing it to your method and assigning it to the event.

On the wider subject of producing code with limited dependencies, I'd suggest studying the SOLID principles.

These are a set of design principles which if followed will pretty much guarantee your code is will written with as few dependencies as possible.

http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)[^]
 
Share this answer
 
Comments
bh_ 8-Oct-13 11:25am    
That's great - fully understood. Now that you've written the delegate assignment in full I can recognise it and it makes perfect sense to me. It also seems pretty obvious now that you've pointed it out!

Thanks for the link as well I will read that with interest.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900