C#C
C#16mo 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 */ }


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


How can I get rid of this warning? Just pragma?
Was this page helpful?