“对象为空”表达产生了一个错误
问题描述:
我有以下代码:“对象为空”表达产生了一个错误
public static class ItemsHelper
{
public static object product
{
get
{
return HttpContext.Current.Items["product"];
}
set
{
HttpContext.Current.Items["product"] = value;
}
}
}
然后,在功能,我有以下表达式:
if (ItemsHelper.product is null) return false;
我在Visual Studio中测试它工作正常,但我测试了两台运行visual studio 2015的不同计算机,它检索到以下错误:
type expected)
任何人都有一个想法,为什么发生这种情况?
答
is null
是一个C#7功能。您需要一个兼容C#7的编译器来编译代码。 VS 2017附带一个兼容的版本,但对于VS 2015,您需要更新。检查此问题:How to use c#7 with Visual Studio 2015?
'is null'是C#7功能,当然它在VS 2015中不存在 – Andrey
为什么不写'ItemsHelper.product == null'? – Sentry
@Sentry我可以,但我想明白为什么会发生这种情况 –