what are events in c# and how is it used with delegates? [Resolved]

Posted by Munder2013 under C# on 5/16/2013 | Points: 10 | Views : 1568 | Status : [Member] | Replies : 7
Hi experts,
Can anyone explain about events in c# ??

Thanks & Regards
Baljeet Singh



Responses

Posted by: aswinialuri-19361 on: 5/17/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
HI Baljeet,
As compared to delegates events works with source and listener methodology. So listeners who
are interested in receiving some events they subscribe to the source. Once this subscription is
done, the source raises events to its entire listener when needed. One source can have multiple
listeners
events do not have a return type.
events are always public .

Actually, events use delegates in bottom. But they add an extra layer on the delegates,
thus forming the publisher and subscriber model.
As delegates are function to pointers, they can move across any clients. So any of the
clients can add or remove events, which can be confusing. But events give the extra
protection by adding the layer and making it a publisher and subscriber model.
Just imagine one of your clients doing this
c.XyzCallback = null
This will reset all your delegates to nothing and you have to keep searching where the error
is

I hope it will use ful to you
Mark as answer if it helps you
Thanks&Regards


Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasik on: 5/17/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
An event is a member that enables an object or class to provide notifications. Clients can attach executable code for events by supplying event handlers.With the event handler syntax in the C# language, we create a notification system. In this way we attach additional methods without changing other parts of the code. This makes programs easier to maintain.

The event keyword is used to create an instance of an event that can store methods in its invocation list.

Below example illustrate the event-delegate example.

using System;

public delegate void EventHandler();

class Program
{
public static event EventHandler _show;

static void Main()
{
// Add event handlers to Show event.
_show += new EventHandler(Dog);
_show += new EventHandler(Cat);
_show += new EventHandler(Mouse);
_show += new EventHandler(Mouse);

// Invoke the event.
_show.Invoke();
}

static void Cat()
{
Console.WriteLine("Cat");
}

static void Dog()
{
Console.WriteLine("Dog");
}

static void Mouse()
{
Console.WriteLine("Mouse");
}
}

Output

Dog
Cat
Mouse
Mouse

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 5/17/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Ashwini,
Thanks for reply my post... But my concept is still not clear
plz provide me some examples

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 5/17/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Rasik,
Thanks for reply my post. plz add comments so i can understand the working of event

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 5/17/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Baljeet,

please refer the below link after verifying you can understand perfectly.
http://www.dotnetfunda.com/articles/article904-6-important-uses-of-delegates-and-events.aspx

I hope you can understand perfectly after seeing this link
Mark as answer if it helps you
Thanks&Regards

Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 5/18/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Ashwini
Thanks for the link

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Munder2013 on: 5/18/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Ashwini
Thanks for the link

Thanks & Regards
Baljeet Singh

Munder2013, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response