Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullable (and resolve most nullable warnings) #119

Open
wants to merge 25 commits into
base: experimental
Choose a base branch
from

Conversation

Hamunii
Copy link
Contributor

@Hamunii Hamunii commented Jul 8, 2024

Enables the Nullable feature of C# in csproj to help better deal with null values.

<Nullable>enable</Nullable>

Basically, this will make you specify if your variable types are nullable or not.

For example, you'll need to write string? something; instead of string something; if the something variable might be null, so the IDE knows to warn you when you try to access the variable and it isn't sure it's not null (This will help catch Null Reference Exceptions much easier). See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types

You can use string something = null!; instead if you know this won't be null when it matters or whatever. See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving

I also tried to resolve most nullable warnings, by quickly looking how each variable with a nullability warning was used, and marked them as nullable if there was code that checked if they were null in other places than just some init methods. A lot of what I did might not be perfect, but it's a lot better than nothing.

There are still a few nullable warnings that I didn't analyze enough (of the ones I did go through), since I marked a thing as nullable because from the code it seemed like there were a lot of null checks for it, but then it's used in some patches without checking for null. So these need to be reviewed a little more closely, but I'm done for today with this.

Also related:
For when(/if) you make more TryGet methods that give back a nullable variable type that is known to be null when return value is false and not null when return value is true, there is an attribute for marking that, e.g. [NotNullWhen(returnValue: true)] out ExtendedDungeonFlow? returnExtendedDungeonFlow: https://stackoverflow.com/a/57627724

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