How to add delegate to c # interface

I need to have some delegates in my class.

I would like to use the interface to “remind” me to install these delegates.

How?

My class is as follows:

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 these delegates:

 public interface myInterface { // ????? } 
+81
c # interface delegates
Oct. 16 '10 at 11:33
source share
6 answers

Delegate types are declared. They do not belong to the interface. Events using these types of delegates can be in the interface, though:

 public delegate void UpdateStatusEventHandler(string status); public delegate void StartedEventHandler(); public interface IMyInterface { event UpdateStatusEventHandler StatusUpdated; event StartedEventHandler Started; } 

An implementation will not (and should not) override the type of delegation, more than it will override any other type used in the interface.

+129
Oct. 16 '10 at 11:40
source share

Starting with .NET 3.5, you can also use System.Action delegates, which will result in the following interface:

 public class ClsPictures : myInterface { // Implementing the IProcess interface public event Action<String> UpdateStatusText; public event Action Started; } 
+28
Aug 28 '12 at 15:19
source share

Just select delegate as property

 public delegate void UpdateStatusEventHandler(string status); public delegate void StartedEventHandler(); public interface IMyInterface { UpdateStatusEventHandler StatusUpdated {get; set;} StartedEventHandler Started {get; set;} } 
+8
Jan 13 '17 at 7:26
source share

John Skeet's answer is right, I just want to add a note.

Interfaces do not exist to “remind” you what to do, or what to include in your classes. Interfaces are abstractions used in object-oriented programming and design methods. Perhaps you do not need an interface declaration at all if you do not want to see some specific instances of the class as an interface elsewhere in your program (abstraction).

If you want to apply some coding standards in your project, you can try using code analysis tools (for example, in Visual Studio). They allow extensions that you can enable to add your own code analysis rules.

Using code analysis, if you “forget” to add delegates (although I don’t see any reason to forget about it, as if the delegate is not used, it is not needed), you will receive a warning / error.

+7
Oct 16 '10 at 12:50
source share

One of your comments indicates the reverse type of event handler. Are you more interested in the type of handler or the data returned from the event? If this is the last, then this may help. If not, then this solution will not be enough, but it can help you get closer to what you are looking for.

All you have to do is declare the event handlers as general event handlers both in the interface and in your implementation, and you can customize the return results.

The class of your class will look like this:

 public class ClsPictures : myInterface { // Implementing the IProcess interface public event EventHandler<UpdateStatusEventArgs> UpdateStatusText; //no need for this anymore: public delegate void UpdateStatusEventHandler(string Status); public event EventHandler<StartedEventArgs> Started; //no need for this anymore: public delegate void StartedEventHandler(); } 

Your interface will look like this:

 public interface myInterface { event EventHandler<StartedEventArgs> Started; event EventHandler<UpdateStatusEventArgs> UpdateStatusText; } 

Now that the event arguments return your types, you can hook them in any handler that you define.

For reference: https://msdn.microsoft.com/en-us/library/edzehd2t(v=vs.110).aspx

+1
Mar 10 '16 at 1:16
source share

The interface inherited in your derived class will remind you to identify and link the material you declared in it.

But you can also use it explicitly, and you still need to associate it with the object.

For example, using the inverse of the control pattern:

 class Form1 : Form, IForm { public Form1() { Controls.Add(new Foo(this)); } // Required to be defined here. void IForm.Button_OnClick(object sender, EventArgs e) { ... // Cast qualifier expression to 'IForm' assuming you added a property for StatusBar. //((IForm) this).StatusBar.Text = $"Button clicked: ({e.RowIndex}, {e.SubItem}, {e.Model})"; } } 

You can try something like this.

 interface IForm { void Button_OnClick(object sender, EventArgs e); } class Foo : UserControl { private Button btn = new Button(); public Foo(IForm ctx) { btn.Name = "MyButton"; btn.ButtonClick += ctx.Button_OnClick; Controls.Add(btn); } } 
0
Nov 15 '15 at 21:33
source share



All Articles