Home > other >  It's possible to prevent CS8602 (variable may be null) as the package owner
It's possible to prevent CS8602 (variable may be null) as the package owner

Time:10-15

For example I have xUnit test

var record = _context.Records.Where(x => x.Foo == FooValue).FirstOrDefault();

// This check WILL NOT prevent the warning from being displayed
Assert.NotNull(record);

// This check WILL prevent the warning from being displayed
if (record == null) throw new Exception(); 

// Warning CS8602 "record may be null here"
Assert.True(record.Foo == FooValue); 

It's possible from the owner of the package (in general) to somehow instruct the compiler that the output of the method is compliant to warning CS8602? I use several packages and I see warnings that are not relevant and I don't want to place

#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8602 // Dereference of a possibly null reference.

everywhere. If it's possible I would fix this at least in the packages I own.

CodePudding user response:

to resolve nullable warning you can use ! in c#

check this : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings

For Example: Assert.True(record.Foo! == FooValue);

  • Related