-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);
3
  • 1
    Please do not upload images of code/data/errors. Commented Aug 25, 2023 at 7:46
  • 1. I've removed the screenshots. I hope everyone understands the issue. Commented Aug 25, 2023 at 8: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. Commented Aug 25, 2023 at 8:02

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.