✅ Possible null reference assignment warning on event unsubscribe
On the following code gives me a
CS8601: Possible null reference assignment.
on build.
From what I can gather, it thinks that View.FocusSectionRequested
is null, but checking that with an if statement just gives me an 'expression is always true' warning.
How can I get rid of this warning? Just pragma?8 Replies
when nothing is subscribed to the event it will be null
you didnt define it as nullable, but when you unsubscribe there, it could be null
i see you tried to work around this by initializing it to a delegate
just define it as nullable and null check when you invoke
thats the standard way of doing ut
just define it as nullable and null check when you invokethat works, thank you. but isn't that a bug in the compiler?
FocusSectionRequested
is never null, because there will always be the empty delegate..when you unsubscribe from all events its null
No, it is not a bug
We did debate this topic actually, repeatedly. Ultimately, it is possible that unsubscribing from an event will turn that event null. In your particular case, a more complicated analysis would reveal that it can't be, but that's a particularly complex analysis, and is effectively equivalent to the halting problem
ok, that makes sense. It's a bit more verbose but i can get rid of the warning like that pretty elegantly. thank you!
@sibber 🇵🇸 thank you too 🙂
you mean trying to analyze at each invokation of the event whether there are subscribers or not?
is equivalent to the halting problem?
Yes
ah ok
oh i just understood your question
i dont know what i was drinking earlier lmao