34

What is a good example of the power of interface events (declaring events inside interface)?

Most of the times I have seen only public abstract methods inside interface.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
user160677
  • 4,233
  • 12
  • 42
  • 55

6 Answers6

57

I used events to signal when a serial port received data.

Here is my interface.

public interface ISerialPortWatcher
{
    event EventHandler<ReceivedDataEventArgs> ReceivedData;
    event EventHandler StartedListening;
    event EventHandler StoppedListening;

    SerialPortSettings PortOptions { set; }

    bool Listening { get; set; }
    void Stop();
    void Start();
}

public class ReceivedDataEventArgs : EventArgs
{
    public ReceivedDataEventArgs(string data)
    {
        Data = data;
    }
    public string Data { get; private set; }
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    .NET's `event` seems to now be unpopular, I suppose because their use-cases are mostly gone (e.g. WinForms isn't popular, WPF prefers MVVM with event bindings in XAML, WebForms is completely dead, and (ab)use of events for async programming in the EAP Pattern is also dead because TPL with `await` is far better) - finally there's a problem inherent in `event` in that it's very easy to forget to de-register and [end-up leaking object-references as `EventHandler` does not use a Weak-Reference](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/weak-event-patterns). – Dai Dec 18 '20 at 11:28
19

An excellent example within the .NET framework is the INotifyPropertyChanged interface. This interface consists of only one member: the PropertyChanged event.

In WPF, you can state that a control will display a specific property of an object instance. But how will this control update if the underlying property changes?

If the bound object implements the INotifyPropertyChanged interface, the WPF framework can just listen to PropertyChanged and update appropriately.

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
13

here is one example

public interface IMainAppWindow
{
   event EventHandler Closed;
}

// version 1 main window
public MainForm : Form , IMainAppWindow
{

}

// version 2 main window
public MainWindow : Window , IMainAppWindow
{
  event EventHandler Closed;

  public void OnClosed(object sender,RoutedEventArgs e)
  {
    if(Closed != null)
    {
      Closed(this,e);
    }
  }
}

I have some code like this in 1 of my applications. The app was written in winforms, then upgraded to WPF.

Andrew Keith
  • 7,515
  • 1
  • 25
  • 41
  • 1
    When raising an event, you should [make a copy of the event](https://msdn.microsoft.com/en-us/library/w369ty8x%28v=vs.100%29.aspx) (see `OnRaiseCustomEvent()`) to avoid the possibility of a **race condition**: `EventHandler handler = this.Closed; if (handler != null) { ... }` – DavidRR Aug 12 '15 at 17:18
5

INotifyPropertyChanged is used through out the framework.

Just look at the INotifyPropertyChanged.PropertyChanged Event

Rohan West
  • 9,262
  • 3
  • 37
  • 64
4

Events in interfaces work pretty much just like methods. You can use them just how you would use any interface.

public interface IInterface {
    event EventHandler QuestionAsked;
}

public class Class : IInterface {
    event EventHandler QuestionAsked;

    //As with typical events you might want an protected OnQuestionAsked
}
Bob
  • 97,670
  • 29
  • 122
  • 130
2

A classic scenario is MVP pattern with passive view. The form implememts an view inteface that has a NameChanged event. The presenter that creates/uses the view subscribes to this event. When the name text in textbox is changed view fires this event. The presenter is then notified. Since the presenter only knows about event from view interface you can provide a mock view for testing. The view is completely decoupled from presenter.

softveda
  • 10,858
  • 6
  • 42
  • 50
  • In the article [Model View Presenter Styles](https://lostechies.com/derekgreer/2008/11/23/model-view-presenter-styles/) which describes three different MVP patterns, the third pattern called **Observing Presenter Style** appears to be most closely aligned with the **Passive View** pattern described here. – DavidRR Aug 12 '15 at 17:27