Why do I get a Warning in the line of list2? I filtered out all null values here. The warning states, that in the select method a null value might be dereferenced.
#nullable enable
using System.Collections.Generic;
using System.Linq;
namespace Secunet.Bocoa.AtBmi.SecondLineCheckPlugin
{
public class Test
{
public Test()
{
List<string?> testList = new List<string?>()
{
"hallo",
null
};
IEnumerable<string> list2 = testList.Where(x => x != null).Select(x => x.Replace("A", "")); // warning
IEnumerable<string> list3 = testList.Where(x => x != null).Select(x => x != null ? x.Replace("A", "") : ""); // no warning
}
}
}
This is the warning I get in the line of list2:
In the line of list3 no warning is issued, but the check in the Select-Statement will always be pointless.
CodePudding user response:
You get a warning because the type in the enumerable is still the nullable string?
.
I suppose it's possible another thread changes the hallo
entry to null
during the enumeration, but more likely the compiler is just not sophisticated enough to understand you have filtered out any possible null
values ahead of time.
You can use .Cast<string>()
after filtering out the null
values to change the type in the enumerable, or you can use a
As for the second scenario, In this case, No warning because of this
This set of warnings alerts you that you're dereferencing a variable whose null-state is maybe-null.
We know for sure that x
won't be null because of the x != null
in Select
.
CodePudding user response:
Try to declare List<string>
and then in Where clasue (x => !string.IsNullOrEmpty(x))