4

Per tags this is an ASP MVC project in Visual Studio 2015. Code in Razor:

@if (Model.AdditionalContacts?.Count > 0) { }

Error received:

Feature 'null propagating operator' is not available in C# 5. Please use language version 6 or greater.

I'm using VS 2015. Why was this not defaulted?

Steps taken to fix:

  • Open Project's Properties
  • Click Build
  • Click Advanced
  • Set Language Version to C# 6.0
  • Clean / Rebuild

Same error occurs. Why?

[Edit]: The code works fine in the CS file directly. I even switched the language back to "default" rather than C# 6.0 and it works. It only does not work in the CSHTML files. Does Razor not support this? Or perhaps this is related to IIS?

svick
  • 225,720
  • 49
  • 378
  • 501
Paul
  • 5,413
  • 5
  • 42
  • 63

1 Answers1

6

C# 6.0 features are not automatically available in MVC5 projects (in Razor), you have two options:

  • Update to MVC 6 beta
  • Install CodeDOM Providers for .NET Compiler, see details here
Community
  • 1
  • 1
Lukas.Navratil
  • 560
  • 4
  • 12
  • 1
    You should update the answer to clarify that it's the Razor *files* that can't use C# 6 features. C# 6 works just fine in the rest of an MVC 5 project. Moreover, if you find a duplicate question, vote to close the current one as duplicate. Multiple answers to identical questions just create noise, making it harder to find the correct answer. – Panagiotis Kanavos Sep 14 '15 at 13:16