NotNullAttribute and event in Nullable Reference Type context? #7121
Replies: 2 comments 2 replies
-
But do you really know? In the 99% case, you won't be certain that a consumer will subscribe to your event, and then it remains null. That's what NRTs are tuned to, so the diagnostics are correct. Anyway, one alternative for you would be to wrap your event with #nullable disable warnings
public event ....
#nullable restore then you won't need the |
Beta Was this translation helpful? Give feedback.
-
This is less about nullable reference types themselves and more about their interaction with inheritance. And if you want to implement the nullable private event PropertyChangedEventHandler propertChangedImpl = delegate {};
public event PropertyChangedEventHandler? PropertyChanged
{
add => propertChangedImpl += value;
remove => propertChangedImpl -= value;
} Wait, what? If If it's intentional, maybe |
Beta Was this translation helpful? Give feedback.
-
Consider a class that implement
INotifyPropertyChanged
:Without
?
the compiler generates a warning ("Nullability of reference types in type of 'event ... PropertyChanged' doesn't match implicitly implemented member 'event PropertyChangedEventHandler? INotifyPropertyChanged.PropertyChanged'"). However now we have to decorate all event invoker with null-forgiving operator!
because we know it is never null. Normally[NotNull]
is the solution but it doesn't applicable for events:Did I miss something here? Should
NotNullAttribute
add Event as a valid target?Beta Was this translation helpful? Give feedback.
All reactions