How to add a delegate to an interface C#

Asaf picture Asaf · Oct 16, 2010 · Viewed 88.7k times · Source

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
{
   // ?????
}

Answer

Jon Skeet picture Jon Skeet · Oct 16, 2010

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.