CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: what is the main difference between delegate and an event in c#?

Answer: 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.



Category C# Interview Questions & Answers - Exam Mode / Learning Mode
Rating (3.8) By 90 users
Added on 9/25/2012
Views 5974
Rate it!
Question: what is the main difference between delegate and an event in c#?
Answer:

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. Source: CoolInterview.com

Answered by: NandaKumar | Date: 10/10/2007 | Contact NandaKumar Contact NandaKumar

Delegates and Events Both are related.

Dalegete is a function pointer which can able to store the address of any function with same prototype.

Event is a function handler which can handles or run the functions in same prototype of its delegate.

For handling events delegate is uesd.

Source: CoolInterview.com

Answered by: A.Mathiyazhagan | Date: 11/16/2007 | Contact A.Mathiyazhagan Contact A.Mathiyazhagan

delelgate is rederence to a method. event is way to access that method using that delegate. Source: CoolInterview.com

Answered by: sandy | Date: 8/12/2008 | Contact sandy Contact sandy

event and delegate are not same .event is a part of delegate.delegate use the event to do somerhing Source: CoolInterview.com

Answered by: sandeep khokhar | Date: 4/25/2009 | Contact sandeep khokhar Contact sandeep khokhar

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.. Source: CoolInterview.com

Answered by: V.JeevanKumar | Date: 5/16/2009 | Contact V.JeevanKumar Contact V.JeevanKumar

Methods are registered in Delegate and Delegates are registered in Events. Source: CoolInterview.com

Answered by: Salman | Date: 5/24/2009 | Contact Salman Contact Salman

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)
{
}
}
Source: CoolInterview.com

Answered by: Sucharit | Date: 6/15/2009 | Contact Sucharit Contact Sucharit

Event keyword provides a better layer of security than delegates.



delegate += f1;
delegate += f2;
delegate = f3 ;// this wipes out the earlier entries of f1 & f2.

This is not possible while using event keyword and the compiler will throw an error.

event = f3; // not allowed
event =null; // not allowed
event (); //not allowed


Source: CoolInterview.com

Answered by: Anand | Date: 8/1/2009 | Contact Anand Contact Anand

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 Source: CoolInterview.com

Answered by: Gurpreet Singh | Date: 11/27/2009 | Contact Gurpreet Singh Contact Gurpreet Singh

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.
Source: CoolInterview.com

Answered by: balakrishna | Date: 12/3/2009 | Contact balakrishna Contact balakrishna

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.

Hope this clarifies Source: CoolInterview.com

Answered by: Raj | Date: 2/7/2010 | Contact Raj Contact Raj

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 pretty confusing. But
events give the extra protection by adding the layer and making it a publisher
and subscriber model. Source: CoolInterview.com

Answered by: rajeshkumarreddychalla | Date: 5/18/2010 | Contact rajeshkumarreddychalla Contact rajeshkumarreddychalla

Delegate is a reference type variable which holds reference to a method and this reference can be changed at runtime. Event is a action. Delegates are used with event to bind the event with event handlers. Source: CoolInterview.com

Answered by: Ujwala | Date: 6/2/2010 | Contact Ujwala Contact Ujwala

delegate is a functional pointer.it holds referece of method.we defind delegate as like same return type and signature of the we required method. Source: CoolInterview.com

Answered by: trimurthy | Date: 6/25/2010 | Contact trimurthy Contact trimurthy


If you have the better answer, then send it to us. We will display your answer after the approval

Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.

Post your answer here

Inform me about updated answers to this question.
Related Questions
View Answer
How to use HASH TABLE,ARRAYLIST in c# explain with example?
View Answer
Assemblies are of the following types:

View Answer
What is the difference between shadow and override

View Answer
What is the top .NET class that everything is derived from?

View Answer
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?

View Answer
When you inherit a protected class-level variable?Who is it available to?

View Answer
Does C# support multiple inheritance?

View Answer

How do you inherit from a class in C#?


View Answer
What is an abstract class?

View Answer
What does the keyword virtual mean in the method definition?

View Answer
How's method overriding different from overloading?

View Answer
What's the top .NET class that everything is derived from?

View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All C# Interview Questions & Answers - Exam Mode / Learning Mode




India News Network
Latest 20 Questions
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
"Holiday Packages" announced as an advertisement are an example of _________: (a) Offer (b) Counter Offer (c) Invitation to Offer (d) None of the above
A match fixing contract between a player and a broker is a: (a) Valid Contract (b) Unenforceable Contract (c) Void Contract (d) Illegal Contract
When a bookseller sells a book on cash payment then it is called as ___________: (a) Unilateral Contract (b) Bilateral Contract (c) Executed Contract (d) Executory Contract
____________ agreements are created by situation: (a) Written (b) Oral (c) Void (d) Implied
Cache = 0.029297 Seconds