943,722 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 47751
  • C# RSS
Sep 16th, 2005
-1

Differences between Delegates and Events

Expand Post »
Hi All,
I would like to know the differences between Delegates and Events in C#.Net.
Both are same or both are diffenent. Which one is preferred?

Thanks and with regards,
Madhusudhan.H.K.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cumadhu is offline Offline
19 posts
since Sep 2005
Sep 16th, 2005
0

Re: Differences between Delegates and Events

They aren't the same. An event is an event. A "delegate" assigns a particular event handler to an object instance. Thus, when an object event happens, the proper procedure/method is called.

An example might help. I frequently code things so that a single method is called for all instances of a class. No matter which button is clicked, call my generic "button_click" method. That method will use the event arguments to determine which button was actually clicked.

In order to assign all button click events to a single method, I have to code the delegates.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Sep 17th, 2005
0

Re: Differences between Delegates and Events

* Event is Arraylist of delegates .
* Delegate ( as its English meaning ) : Secretary or someone who connect two persons throuh him .
* Delegate can fire only one method
But Event fire more than one delegate , each call one method

* Example : in Windows App. or ASP.NET App.
Button : is a class that has internally an event called ( eg. click ) and there is
a method you want to do if this button clicked so make an instance of the delegate
which button.click deal with ( System.EventHandler ) and make this delegate
execute the method eg. Button1_Click

Code :
in initializer of The Form Class :
Button1.Click += new System.EventHandler(this.button1_Click) ;
And make amethod :
private void button1_Click(object sender, System.EventArgs e)
{
// write your own code
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mostafa gaber is offline Offline
5 posts
since Sep 2005
Sep 19th, 2005
0

Re: Differences between Delegates and Events

Thanks a for kind information.
I know the exact difference.

Once Again a lot to all.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cumadhu is offline Offline
19 posts
since Sep 2005
Jul 23rd, 2010
-1
Re: Differences between Delegates and Events
Event is a C# Modifier.
Delegate is a C# type.

delegate is a type that references a method.

Event is a Modifier used to declare a special kind of multicast delegate that can only be invoked from within the class or struct where it is declared (the publisher class).

Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Itzikg is offline Offline
1 posts
since Jul 2010
Jul 23rd, 2010
0
Re: Differences between Delegates and Events
Whilst that was definitely a very well worded response (kudos).
This thread has been dead for almost 5 years, i'm not sure the OP is still waiting
Last edited by Ryshad; Jul 23rd, 2010 at 6:48 am.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 12th, 2010
0
Re: Differences between Delegates and Events
All these theoritical differenes are OK. I want to know what is the difference in use. Why cannot we use multicast delegate to invoke more then one method rather then event.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
r4rakesh is offline Offline
1 posts
since Nov 2010
24 Days Ago
0
Re: Differences between Delegates and Events
you can use a multicast delegate instead of an event. but the main reason for using an event is that it prevents the invoking ability from its subscribers. basically events promote encapsulation over multicast delegates.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elninoisback is offline Offline
1 posts
since Jan 2012
Message:
Previous Thread in C# Forum Timeline: Get last portion of server name
Next Thread in C# Forum Timeline: Windows Forms Menu (XNA Style :: Re-Release)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC