There are many questions about events in interfaces. Here are a couple:
- http://stackoverflow.com/questions/471352/raising-events-in-interfaces
- http://stackoverflow.com/questions/1093139/c-events-and-interfaces
As interfaces are used to enforce a contract for implementation, this makes no sense to me, because it doesn't enforce the actual raising of the event. The specific implementation can always have the event, but not raise it anywhere. Consider the following:
public interface INotifyPropertyChanged
{
event PropertyChangedEventHandler PropertyChanged;
}
public class SomeClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string SomeProperty
{
get { return this.someField; }
set { this.someField = value; }
}
private string someField;
}
The thing above will compile and work, but even if I subscribe to the PropertyChanged event, nothing will happen. What would be a way to enforce that an event is actually raised, and if not, why have events in interfaces in the first place?