Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using the null-propagating / null-conditional operator

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

I found this code on this MSDN blog.

like image 222
Old Geezer Avatar asked Apr 08 '15 14:04

Old Geezer


1 Answers

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

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).

like image 192
Patrick Hofman Avatar answered Sep 23 '22 15:09

Patrick Hofman