Home > other >  Dereference of a possibly null reference inside Net6 IF statement
Dereference of a possibly null reference inside Net6 IF statement

Time:12-04

Using NET 6 with <Nullable>enable</Nullable> in my project definition I have:

if (!(await _context?.Applications.AnyAsync())) {

}

I get the tip "_context might be null here" and this why I'm using ? on it.

I am getting a warning:

Dereference of a possibly null reference.

I also tried:

if (!(await _context?.Applications.AnyAsync()) ?? false) {

}

But that does not compile. How can I solve this?

CodePudding user response:

Try this:

if (_context != null && !(await _context.Applications.AnyAsync())) {

}
  • Related