I am trying to use the default interface methods
feature in .NET 9, C# 13.
Consider the following interface:
public interface ICustomNotifyPropertyChanged:
INotifyPropertyChanged
{
public void OnPropertyChanged (string propertyName);
// Compiler Error: [CS0079].
//public void OnPropertyChanged (string propertyName)
// => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected bool SetField<T> (ref T field, T value, [CallerMemberName] string propertyName = "")
{
if (EqualityComparer<T>.Default.Equals(field, value)) { return (false); }
field = value;
this.OnPropertyChanged(propertyName);
return (true);
}
}
Notice how the default implementation attempt of OnPropertyChanged
causes the following error:
CS0079: The event 'ICustomNotifyPropertyChanged.PropertyChanged' can only appear on the left hand side of += or -=.
This question is not about whether the usage context is appropriate, but rather why triggering an event is not allowed. I get that default implementations have been designed for interoperability albeit often used for convenience. However, on top of everything that default implementations ARE allowed to do, why not trigger an event? Are there undesirable situations that could arise if this were allowed? I am not sure if I worded the question title appropriately.
The main reason is instance fields aren't permitted in interfaces, see interface keyword.
Events are similar to auto-implemented properties, consisting of a private delegate field and an event definition that includes add and remove methods Therefore, to invoke an event, it is actually calling the Invoke method of the delegate.
Because there is no that private field in the interface definition, you cannot invoke it either.
However static fields are permitted, so you can invoke static events in interfaces.