Skip to content

Fix Action delegate's nullability#77

Merged
XuuXiaolan merged 1 commit intoTeamXiaolan:mainfrom
ratijas:work/ratijas/null-delegate
Feb 5, 2026
Merged

Fix Action delegate's nullability#77
XuuXiaolan merged 1 commit intoTeamXiaolan:mainfrom
ratijas:work/ratijas/null-delegate

Conversation

@ratijas
Copy link
Contributor

@ratijas ratijas commented Feb 5, 2026

In AchievementUIGetCanvas destructor removes a delegate from DuskAchievementHandler.OnAchievementUnlocked, which triggers a warning about possible null assignment.

    protected override void OnDestroy()
    {
        DuskAchievementHandler.OnAchievementUnlocked -= QueuePopup;
    }

Turns out, delgates implicitly implement -= operator in a way that "If removal results in an empty list, the result is null."1 So from static typing POV, it doesn't matter that OnAchievementUnlocked action is initialized with an empty delegate { } — it still sees the code as nullable.

The fix is to consistently treat such action delegate as nullable. That also means that a dummy delegate { } initialization is redundant.

In AchievementUIGetCanvas destructor removes a delegate from
DuskAchievementHandler.OnAchievementUnlocked, which triggers a warning
about possible null assignment.

    protected override void OnDestroy()
    {
        DuskAchievementHandler.OnAchievementUnlocked -= QueuePopup;
    }

Turns out, delgates implicitly implement `-=` operator in a way that "If
removal results in an empty list, the result is `null`."[1] So from static
typing POV, it doesn't matter that `OnAchievementUnlocked` action is
initialized with an empty `delegate { }` — it still sees the code as
nullable.

The fix is to consistently treat such action delegate as nullable.
That also means that a dummy `delegate { }` initialization is redundant.

[1]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/subtraction-operator#delegate-removal
@XuuXiaolan XuuXiaolan merged commit fd0190e into TeamXiaolan:main Feb 5, 2026
1 check passed
@ratijas ratijas deleted the work/ratijas/null-delegate branch February 5, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants