显示可能的空引用警告的取消引用的标准是什么?

What is the criteria for showing a dereference of a possible null reference warning?

提问人:Predrag Despotovic 提问时间:9/2/2022 最后编辑:Jon SkeetPredrag Despotovic 更新时间:9/2/2022 访问量:64

问:

我的洋葱微服务架构中有一个 QueryExecutor 文件,我在下面的代码中收到了可能为空引用警告的取消引用:

public async Task<IReadOnlyCollection<T>> GetAll<T>() where T : BaseEntity
    => await _mongoDbContext.GetCollection<T>()
                            .Find(_ => true).ToListAsync();

public async Task UpdateAsync<T>(FilterDefinition<T> filterDefinition, UpdateDefinition<T> updateDefinition) where T : BaseEntity
    => await _mongoDbContext.GetCollection<T>()
                            .UpdateOneAsync(filterDefinition, updateDefinition);

只有 Update 方法发出警告,我想我理解它 - mongoDbContext.GetCollection() 可以返回一个 null 值。但我不明白的是为什么 GetAll 方法不返回它。我看到的唯一区别是 Update 返回值是一个 void Task,而 Update 有一个只读集合。

C# 警告 NET-6.0

评论

0赞 Silenus 9/2/2022
GetAll 方法不是返回一个空集合而不是 null 吗?
0赞 Predrag Despotovic 9/2/2022
GetAll 返回集合中的所有文档,如果这是你的意思。如果 GetAll 方法中的 _mongoDbContext.GetCollection<T>() 表示,则使用 CollectionName 属性返回由 T 类型指定的 Collection。

答: 暂无答案