相关疑难解决方法(0)

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

在 C# 8 项目中,我正在使用可为空引用类型并收到一个意外的(或至少对我来说是意外的)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")}";
}
Run Code Online (Sandbox Code Playgroud)

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

请注意,类型x.DataIntint?

c# c#-8.0 nullable-reference-types

7
推荐指数
1
解决办法
1173
查看次数

标签 统计

c# ×1

c#-8.0 ×1

nullable-reference-types ×1