提问人:Muflix 提问时间:10/10/2022 更新时间:10/10/2022 访问量:565
可以阻止 CS8602(变量可能为 null)作为包所有者
It's possible to prevent CS8602 (variable may be null) as the package owner
问:
例如,我有 xUnit 测试
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);
包的所有者(通常)可以以某种方式指示编译器该方法的输出符合警告 CS8602?我使用了几个包,但我看到不相关的警告,我不想放置
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
到处。如果可能的话,我至少会在我拥有的软件包中解决这个问题。
答:
4赞
Curllog
10/10/2022
#1
要解决可为 null 的警告,您可以使用 !在 C 中#
例如: Assert.True(记录。呜呜!== FooValue);
评论
_context.Records.Where(x => x.Foo == FooValue).First();
?