C # Delegates and Method Signatures

From MSDN :

Any method that matches the signature, which consists of the return type and parameters, can be assigned to the delegate.

So how is this possible:

public delegate void AlarmEventHandler(object sender, EventArgs e); public event AlarmEventHandler Alarm; protected virtual void OnAlarm(EventArgs e) { AlarmEventHandler handler = Alarm; if (handler != null) { // Invokes the delegates. handler(this, e); } } 

the AlarmEventHander delegate and the AlarmEventHander event have different signatures, but a handler can be assigned an Alarm .

Perhaps I misunderstand the delegates somewhat, and I would be very grateful if anyone could explain where I am going wrong.

+7
source share
5 answers

A delegate is a class. The event is similar to a property. When you declare an event in a class, you declare an event type. In this case, AlarmEventHandler , which is the inner class of the top-level class, is part.

In the OnAlarm method OnAlarm you get the instance of the AlarmEventHandler class that was assigned to this event, and call it.

To clear everything up, your code above is similar to this using regular classes and links:

 public class InnerClass { public void MyMethod() { /* ... */ } } public InnerClass MyProperty { get; set; } protected virtual void CallMyMethod() { InnerClass cls = MyProperty; if (cls != null) cls.MyMethod(); } 
+7
source

Actually, the signatures are the same. In .NET, events are implemented with delegates.

 public event AlarmEventHandler Alarm; 

Thus, the above code is actually compiled by the compiler as:

 private AlarmEventHandler handler; public event AlarmEventHandler Alarm { add { handler += value; } remove { handler -= value; } } 

Thus, the event actually uses the same delegate AlarmEventHandler .

+2
source
  • You mix delegates and events. While events depend on delegates, they are difference concepts. Therefore, it should be taken separately.
  • Delegation is like a type definition. The place where you use this delegate is like a variable or property.
  • The event makes this delegate behave differently from the outside. The signature is still the same.
0
source

An event is just a delegate. A delegate has access only from within the class. From the outside, it is only possible to add and remove functionality for this event, you can only do this:

 myAlarm.Alarm+=new AlarmEventHandler(callPolice); // or myAlarm.Alarm-=new AlarmEventHandler(callPolice); 

But from inside the Alarm class is just a delegate of type AlarmEventHandler, so you can do what your code shows:

  AlarmEventHandler handler = Alarm; if (handler != null) { // Invokes the delegates. handler(this, e); } 
0
source

A function that takes a base class as a (non-referenced) parameter can be converted to a delegate that takes a derived class as a parameter. This is because a function that takes a base can be safely replaced when a function that takes a derived class is used.

 void TakesObject(object o) { ... } Action<string> myAction=TakesObject; 

You can only call myAction by passing a string. And since each row is an object, the contract for TakesObject is executed.

In your case, it works because each AlarmEventArgs also an EventArgs . Thus, the contract requirements for your event handler are less stringent than the delegate-type contract guarantees used in this event.

This is called co- and contra-dispersion. You have joint variance in return types and a contradiction in parameters.

Check out this article on MSDN:
Using Difference in Delegates (C # and Visual Basic)

-3
source

All Articles