Getting Rid of Nullability Warnings
Hi, I currently have a lot of warnings with nullability enabled.
For example, database entities, where I am sure that
string
s are required in the table, how to I get rid of the warning?9 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Here are some examples of how to solve different problems, but I urge to handle each case as it should be (there are situations where you can mark code with
!
to tell the compiler that you (the developer) ensure that it's not null.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warningsResolve nullable warnings
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
= string.Empty;
what about
Expression is always true according to nullable reference types' annotations
when it actually can be null, for example Model
in a View
?you probably activated nullable-reference-types
to indicate a parameter can be null you have to add a
?
behind it's type, ie MyViewModel? model
If you are satisfied with response, close this post and open new one with question.
I'm referring to MVC's cshtml file where we can have
@model MyModel
and it doesn't seem to be nullable. But in some cases, there are cases where the model is null when the view is rendered
or maybe i'm not supposed to do that?
you can add a
?
there too, to indicate that the type is nullable:
@model MyModel?
Ooh
thanks a lot 😄