C
C#•3y ago
kunio_kun

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 strings are required in the table, how to I get rid of the warning?
9 Replies
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
ConfusedPenguin
ConfusedPenguin•3y ago
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-warnings
Resolve nullable warnings
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
xyrile
xyrile•3y ago
= string.Empty;
kunio_kun
kunio_kunOP•3y ago
what about Expression is always true according to nullable reference types' annotations when it actually can be null, for example Model in a View?
undisputed world champions
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
ConfusedPenguin
ConfusedPenguin•3y ago
If you are satisfied with response, close this post and open new one with question.
kunio_kun
kunio_kunOP•3y ago
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?
<div id="static-configuration" class="@(Model is not null && Model.Eth0EnableDhcp ? "d-none" : "")">
<div id="static-configuration" class="@(Model is not null && Model.Eth0EnableDhcp ? "d-none" : "")">
undisputed world champions
you can add a ? there too, to indicate that the type is nullable: @model MyModel?
kunio_kun
kunio_kunOP•3y ago
Ooh thanks a lot 😄
Want results from more Discord servers?
Add your server