C
C#3mo ago
sneki

✅ Possible null reference assignment warning on event unsubscribe

On the following code gives me a CS8601: Possible null reference assignment. on build.
// View.razor.cs
internal EventHandler<FocusSectionEvent> FocusSectionRequested = delegate { };

// Section.razor.cs
[CascadingParameter] public View View { get; set; } = default!;

protected override void OnInitialized() => View.FocusSectionRequested += OnFocusSection; // No warning
public void Dispose() => View.FocusSectionRequested -= OnFocusSection; // warning CS8601: Possible null reference assignment.

private void OnFocusSection(object? sender, FocusSectionEvent e) { /* stuff */ }
// View.razor.cs
internal EventHandler<FocusSectionEvent> FocusSectionRequested = delegate { };

// Section.razor.cs
[CascadingParameter] public View View { get; set; } = default!;

protected override void OnInitialized() => View.FocusSectionRequested += OnFocusSection; // No warning
public void Dispose() => View.FocusSectionRequested -= OnFocusSection; // warning CS8601: Possible null reference assignment.

private void OnFocusSection(object? sender, FocusSectionEvent e) { /* stuff */ }
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.
PS $> dotnet --info
.NET SDK:
Version: 8.0.206
Commit: bb12410699
Workload version: 8.0.200-manifests.afa40ca5
PS $> dotnet --info
.NET SDK:
Version: 8.0.206
Commit: bb12410699
Workload version: 8.0.200-manifests.afa40ca5
How can I get rid of this warning? Just pragma?
8 Replies
sibber
sibber3mo ago
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
sneki
snekiOP3mo ago
just define it as nullable and null check when you invoke
that works, thank you. but isn't that a bug in the compiler? FocusSectionRequested is never null, because there will always be the empty delegate..
sibber
sibber3mo ago
when you unsubscribe from all events its null
333fred
333fred3mo ago
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
sneki
snekiOP3mo ago
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 🙂
sibber
sibber3mo ago
you mean trying to analyze at each invokation of the event whether there are subscribers or not? is equivalent to the halting problem?
333fred
333fred3mo ago
Yes
sibber
sibber3mo ago
ah ok oh i just understood your question i dont know what i was drinking earlier lmao
Want results from more Discord servers?
Add your server