Difference in calling through delegates and C # events

What is the difference?

Using Delegate

public delegate void TestDelegate(); public TestDelegate delObj = SomeMethod; public void SomeMethod() { ..... } public void Test() { if(delObj != null) delObj(); } 

Event use

 public delegate void TestDelegate(); public event TestDelegate EdelObj += SomeMethod; public void SomeMethod() { ..... } public void Test() { if(EdelObj != null) EdelObj(); } 

Both seem to work. Can someone explain what the difference is and which scenario should we use one of the above?

Change Chains work for both. Sorry, this was a mistake from my end.

Thanks Nishant

+4
source share
5 answers

An event declaration is simply a special kind of property that is used to display a delegate. Instead of receiving and installing accessors, it creates and removes them. They usually execute automatically, but if you want to add custom behavior:

 private MyEventHandler handler; public event MyEventHandler MyEvent { add { handler += value; Trace.WriteLine("MyEvent handler attached."); } remove { handler -= value; Trace.WriteLine("MyEvent handler removed."); } } 

It does two things. First, since events are properties, they can be included in interfaces. Secondly, since MyEvent does not return a value, the delegate is fully encapsulated, and only the object calling it can call it. Other ways to display delegates make it possible for a delegate to be called by anyone.

Besides this bit of special language support, another significant difference between events and delegates is more a convention than a language or structure: events are expected to follow certain patterns , for example, make sure the delegate on which the event is based follows the pattern set by the EventHandler delegate .

As a rule, events are preferable whenever semantics refers to an object that notifies anyone who is interested in changing its stage. Delegation is preferable in situations where you want others to be able to define behavior by providing their own procedure, such as delegate parameters, that are executed by many extension methods in IEnumerable that are used in LINQ.

+2
source

It works the exact same way. If you use

 public event EventHandler SomeEvent; 

C # compiler does (simplified code):

 private EventHandler SomeEventField; public void add_SomeEvent( EventHandler handler) { this.SomeEventField = (EventHandler)Delegate.Combine(this.SomeEvent, handler); } public void remove_SomeEvent( EventHandler ) { this.SomeEventField = (EventHandler)Delegate.Remove(this.SomeEvent, handler); } 

The add_SomeEvent method add_SomeEvent called when you use SomeEvent += ... , and remove_SomeEvent is called when using SomeEvent -= ...

However, in both cases the same delegates are used.

+2
source

This is not delegation vs event, the event is a peculiar property around the delegate.

In general, you should always use the second script, because in the first public TestDelegate delObj there is a public field, interrupting Encapsulation.

You use events in the class interface, delegates as parameters for methods.

+1
source

It effectively wraps properties around an event.

The main difference is that you cannot call / raise an event from outside the class, while you can call a public delegate.

Chains (+ =) are no different for delegates and events.

+1
source

There are many constructs in C # that do not add anything new to this language, but are just syntactic sugar for the construct, because the construct requires some kind of code template that is tedious to type over and over again. For example, the using statement is equivalent to a try-finally block with a call to the dispose method, and the properties are just syntactic sugar for the get and / or set method.

Similarly, events are just syntactic sugar for the delegate field. However, there are some subtle differences that others have spoken about. Since the public field is not recommended, the delegate must be encapsulated, and a good way to do this with the event. And (as Mark points out), an event is only invokable from within the class.

I usually choose between a delegate field and an event based on what I conceptually believe the field or event should represent. If he presents something that someone would like to be notified about, I would recommend this event. Otherwise, if it is, for example, a function to get the result, I would use the delegate field.

0
source

All Articles