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.
Delegate: Normally funtions we called in oop's by object of the class.so each time fuction stack will creats and execute that function so each time memory will allocates for this exection..so memory will be wasting here., so to resolve this problem Delegate is came Actually, Delegate is a function pointer it creates funtion stack and exucutes the functions on a same memory location(number of times you called also).so no separate memory allocation is required each and every time to call a same function..Delegates takes funtion as an argument.
Event: Event is an action ,it runs on a perticular time only..for example:when button click,mouseoveron like
So finally we considerd as events and delgates are not comparible..
Event is a type of delegate ( a mUlticast delegate). From usage perspective Delegate and Event has a difference . A public instance of a delegate can be used from outside the class to be raised where as Events are not allowed to be raised outside the class where it is declared.
For example you have class where you have a public instance of a delegate and and a event of the same delegate type
public delegate void TestDel(Object seder,string args); public class DelegateEventTest { public TestDel aDelegate;
public event TestDel aEvent; }
Now from your main method which is in a separate class you can raise delegate but you cannot raise the event . Evet can only be raised within the same class(here class DelegateEventTest).
class Program { static void Main(string[] args) { DelegateEventTest test = new DelegateEventTest(); test.aDelegate = new TestDel(MethodtoTestDelegate); /* This line will never compile*/ // test.aEvent = new TestDel(MethodtoTestDelegate); }
static void MethodtoTestDelegate(Object o, string a) { } }
Basically Delegate is reference type object that can hold reference of a method. and Event is occurence in specific condition. BUT delegates and events are related. We can not fire any event without delegate. Delegate associates eventhadler(method) with event which will be invoked when that particular event will be fired. We can execute more then one functions/methods with the help of delegate on event occurence. BUT POINT TO KEEP IN MIND AS METHODS TO BE PASSED IN DELEGATE SHOULD HAVE SAME SIGNATURE OF DELEGATE EVEN THEN RETURN TYPE SHOULD ALSO BE SAME. hence we can say delegates and events are co-related
their is no similarity b/w a delegate and event. delegates are similar to function pointers in our c and c++. a delegate can hold a function reference,and the operations on delegates effects or call the functions.delegates mainly used in event handling where as events use delegate concept.
An Event is an occurence of an event. It may be arrival of an email, Clicking of a button, Binding of a Control etc.,
Now, given the fact that event is nothing but signalling of something happening, next logical question what we should do when such event happens. Every even we take care enough to define should be associated with an action that should be performed when said event happens. These actions are known as event handlers. In the above cases, the event handlers may be putting email in Inbox, opening up of a popup or formatting of data source etc.,
As many would have guessed, Event handlers should be function. We associate a function( or set of functions ) to be executed when event occurs. This is where delegate comes in. A delegate is a type ( like int, string etc.,) which holds the names of the functions. A further restriction is that a particular delegate variable can hold names of functions with particular signature. We declare an event and provide it a delegate variable which contains name of the function. Thus Event and Event handler are associated.
C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?