'Dereference of a possibly null reference ... Can my code be simplified?
My project is .Net-6 Blazor WebAssembly (hosted) in C#. Can my code be simplified to avoid nullable warnings? I want the person's customer-ID in a page variable from the ApplicationUser object's Identity Name (variable is '_Name'). Thanks.
List<Person> listPersons = (List<Person>)(await PService.GetPersons()).ToList();
Person oPerson = new Person();
if (listPersons != null){
oPerson = (Person)listPersons.Where(p => p.Name!.Equals(_Name)).FirstOrDefault();
}
if (oPerson != null) {
_UID_CUSTOMER = oPerson.UID_CUSTOMER;
}
Solution 1:[1]
You can avoid nullable warning by removing this setting from csproj file
<Nullable>enable</Nullable>
And have these settings
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Surinder Singh |
