在 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.DataInt为int?。