I need to have some delegates in my class.
I'd like to use the interface to "remind" me to set these delegates.
How to?
My class look like this:
public class ClsPictures : myInterface { // Implementing the IProcess interface public event UpdateStatusEventHandler UpdateStatusText; public delegate void UpdateStatusEventHandler(string Status); public event StartedEventHandler Started; public delegate void StartedEventHandler(); }
I need an interface to force those delegates:
public interface myInterface { // ????? }
It's the base of design in the Object Oriented Programming. These are basically declaring delegate types, so they don't belong in an Interface. You can use Event in Interface with Delegate type.
An interface method can accept a delegate as a parameter, no issues.
Interface calls are faster than delegate calls. An interface reference is a reference to an instance of an object which implements the interface. An interface call is not that different from an ordinary virtual call to an method. A delegate reference, on the other hand, is a reference to a list of method pointers.
An interface can declare an event. The following example shows how to implement interface events in a class. Basically the rules are the same as when you implement any interface method or property.
Those are declaring delegate types. They don't belong in an interface. The events using those delegate types are fine to be in the interface though:
public delegate void UpdateStatusEventHandler(string status); public delegate void StartedEventHandler(); public interface IMyInterface { event UpdateStatusEventHandler StatusUpdated; event StartedEventHandler Started; }
The implementation won't (and shouldn't) redeclare the delegate type, any more than it would redeclare any other type used in an interface.
Since .NET 3.5 you can also use the System.Action delegates, without the need to declare your own type.
This would result in the following interface:
public interface myInterface { // Implementing the IProcess interface event Action<String> UpdateStatusText; event Action Started; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With