-3

can anyone tell why the compiler is throwing the warning even if i checked everthing for null??

// check if non default accent color is selected
var accCol = await LocalSorage.GetItemAsync<string>("accentColor");
if (!String.IsNullOrEmpty(accCol) && aColors!=null && aColors.accentColorsList!=null && aColors.accentColorsList.Count()>0)
{
    AccentColors res = aColors.accentColorsList.Find(x => x.ColorValue == accCol);
    if(res!=null) {
        this.selOption = res;
    }
    this.mAccentBaseColor = accCol;
}

VsCode is throwing the following warning over the linq query: onverting null literal or possible null value to non-nullable type.Roslyn CS8600

As i am not allowed to post a screenshot of the warning, the warning is throwed for this line:

AccentColors res = aColors.accentColorsList.Find(x => x.ColorValue == accCol);
  • 1
    [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – Julian Aug 25 '23 at 07:46
  • 1. I've removed the screenshots. I hope everyone understands the issue. – Sascha Heimann Aug 25 '23 at 08:01
  • @shingo sure I konw about how to disable the nullable warnings in csproj. But thats not my question. I want to know why the compiler is throwing the warning, even I've checked for null already. – Sascha Heimann Aug 25 '23 at 08:02

1 Answers1

0

Ok, i found the solution by myself. I've to declare the Linq result as nullable.

// check if non default accent color is selected
var accCol = await LocalSorage.GetItemAsync<string>("accentColor");
if (!String.IsNullOrEmpty(accCol) && aColors!=null && aColors.accentColorsList!=null && aColors.accentColorsList.Count()>0)
{
    AccentColors? res = aColors.accentColorsList.Find(x => x.ColorValue == accCol);
    if(res!=null) {
       this.selOption = res;
    }
    this.mAccentBaseColor = accCol;
}