Nullable reference types and events #3016
Replies: 8 comments
-
I guess it doesn't really matter, since even when the event is declared as not nullable, no warning is emitted for |
Beta Was this translation helpful? Give feedback.
-
Is there a particular reason why you want to forbid |
Beta Was this translation helpful? Give feedback.
-
I guess not... It's just that it doesn't feel right. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I think the compiler suggesting that events should be nullable is a good thing. It reminds you to invoke them in a null-safe way. However it also lets people who want to follow this pattern do so: public event EventHandler Foo = delegate { }; Also: private EventHandler? _foo;
public event EventHandler Foo
{
add { _foo += value; }
remove { _foo -= value; }
} Watch out: this isn't thread-safe. (You probably knew that already, but others reading this might not.) |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I understood this issue to be specifically about field-like events. If you're writing custom accessors, there doesn't seem to be any issue with nullability. |
Beta Was this translation helpful? Give feedback.
-
I read the OP as NRTs don't currently handle field-like events well, in that either you declare it as non-nullable and the compiler complains that you're not initializing it (which makes no sense), or you declare it as nullable and the accessors don't warn when passing Either way, to shut up the compiler you're forced to adorn all events as nullable, and that seems silly. |
Beta Was this translation helpful? Give feedback.
-
@svick, my issue is perfectly summarized by @HaloFour's last comment. I know I can work around it with custom accessors, but it's a pain to write correctly (if you want to account for thread safety, at least). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to get my head around something... With nullable context enabled, if I declare an event like this:
I get a warning because it's not initialized, which makes sense. But I don't have any relevant value to initialize it with, so I'd rather make it nullable. But if I do this:
It quells the warning, but it seems to imply that
null
would be a valid handler for this event, even though it's not.I guess I could apply different nullability to the event itself (not nullable) and the backing field (nullable). Something like this:
But it's a shame to have to do this, I'd rather keep using field-like events. The snippet above is annoying to write, and doesn't even do the same thing as a field-like event (which is a bit more complex to take care of thread safety).
So what's the recommended way to declare events with nullability enabled?
It's probably too late now, but I think it would have been nice if field-like events worked like my snippet above:
Beta Was this translation helpful? Give feedback.
All reactions