在 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
不能为空,如果singleContent
已检查。
请注意,类型x.DataInt
is int?
.
这只是一个临时答案,直到可为空引用类型的设计者之一 Julien Couvreur 发布明确的答案(唠叨)。我在这里发布这个问题是因为一旦 C# 8 发布,这个问题将会再次被问到。
正如 Julien 在 3 个 Github 问题中的回答#34800 https://github.com/dotnet/roslyn/issues/34800 , #37032 https://github.com/dotnet/roslyn/issues/37032 and #36149 https://github.com/dotnet/roslyn/issues/36149这是 C# 8 分析器的已知限制,超出了 C# 8 的范围。
这需要别名分析(这里只是猜测)意味着分析器将能够分析别名表达式,即其结果“隐藏”在临时变量(也可能是参数?)后面的表达式。
也许我们可以在线询问他或 Mads Torgersen .NET Core 3 何时发布.NET 大会 2019 https://www.dotnetconf.net/关于发布日期(完全没有唠叨)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)