使用null传播/null条件运算符时发生错误 [英] Error when using the null-propagating / null-conditional operator

查看:59
本文介绍了使用null传播/null条件运算符时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VS 2013中运行.NET 4.5项目.为什么以下代码错误?

I am running a .NET 4.5 project in VS 2013. Why is the following code in error?

var w = Request.Properties["MS_HttpContext"] as System.Web.HttpContextWrapper;
string IP = w?.Request.UserHostAddress; //fail to compile

我在推荐答案

这是C#6和更高版本中可用的新功能.它称为空条件运算符.

That is a new feature available in C# 6 and newer versions. It is called the null-conditional operator.

要使用C#6,您应该下载Visual Studio 2015或更高版本,因为不再支持Visual Studio 2013的扩展名(请参见此处).

In order to use C# 6 you should download Visual Studio 2015 or a newer version, since the extension for Visual Studio 2013 isn't supported any more (see here).

这篇关于使用null传播/null条件运算符时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆