Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I have a problem to understand the event handling.
I have read delegates and understood it, but please support me with a small example to understand how event handling is different from delegates.
Thank you very much for your help, i would really appreciate it

Please see my recent answer to a similar question:
Since we have multicast delegates, why do we need events?[^].

Share this answer
Read and Repeat Aloud:
A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback. While delegates have other uses, the discussion here focuses on the event handling functionality of delegates...
An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a mouse click, or it could be triggered by some other program logic. The object that raises (triggers) the event is called the event sender. The object that captures the event and responds to it is called the event receiver.

More information:[^]

What are the differences between delegates and events?
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.[^]

And More?
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.[^]

I hope it's your last question about this subject! ;)
Share this answer
Sander Rossel 19-Mar-12 17:25pm    
Great answer! My 5.
Clifford Nelson 19-Mar-12 17:39pm    
That is just more of what he sees in books. He asked for a smaple, not a regurgitation of book information.
Shahin Khorshidnia 19-Mar-12 18:08pm    
Check the links Clifford. There are samples.
And Also check OP's prevoius question, I wrote a sample too.
I think you may revote my solution ;)
public class SomeClass
    public delegate void SampleDelegate(object sender, EventArgs arg);

    public event SampleDelegate SampleEvent;

    protected void OnSampleEvent(string message)
        if (SampleEvent != null)
            SampleEvent(this, new EventArgs());

public class SomeOtherClass
    private SomeClass someClass;

    private void SomeMethod()
        someClass.SampleEvent += new SampleDelegate(MainWindow_SampleEvent);

    private void SomeOtherClass_SampleEvent(object sender, EventArgs arg)
        throw new NotImplementedException();

Basically want to have some code executed in SomeOtherClass when something happens in SomeClass, that causes OnSampleEvent to be called. Need to check for null because will get an error if nobody has subscribed to SampleEvent. Since SomeOtherClass has subscribed to SampleEvent, this will not happen. Once OnSampleEvent has been called,then the SomeOtherClass_SampleEvent will be called.

Hopefully that will help.
Share this answer

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