可空引用类型意外 CS8629 可空值类型可能为空且带有临时变量

Nullable reference types unexpected CS8629 Nullable value type may be null with temporary variables

在 C# 8 项目中,我正在使用可为 null 的引用类型并收到意外(或者至少对我来说是意外的)CS8629 警告,

bool singleContent = x.DataInt != null;
bool multiContent = x.DataNvarchar != null;

if (singleContent && multiContent)
{
    throw new ArgumentException("Expected data to either associate a single content node or " +
        "multiple content nodes, but both are associated.");
}

if (singleContent)
{
    var copy = x.DataInt.Value; // CS8629 here
    newPropertyData.DataNvarchar = $"umb://{type.UdiType}/{Nodes[copy].UniqueId.ToString("N")}";
}

我决定使用 GetValueOrDefault() 作为解决方法,但我想知道如何向编译器证明 x.DataInt 不能为 null 如果 singleContent已选中。

注意x.DataInt的类型是int?

这只是一个临时答案,直到可空引用类型的设计者之一 Julien Couvreur 发布了最终答案(唠叨)。我将其发布在这里是因为一旦 C# 8 发布,该问题将再次被提出。

正如 Julien 在 3 Github 问题 #34800 , #37032 and #36149 中的回答,这是 C# 8 分析器的一个已知限制,它超出了 C# 8 的范围。

这需要别名分析(这里只是猜测)意味着分析器将能够分析别名表达式,即结果在 "hidden" 后面的临时变量(也可能是参数?)的表达式。

也许我们可以在 .NET Conf 2019 期间发布 .NET Core 3 时在线询问他或 Mads Torgersen 关于发布日期(完全不是唠叨)