4

I am getting this annoying error when trying to use a simple interpolated string in my cshtml file:

@for (int i = 0; i < ppTitles.Count; i++)
{
    <p>@ResourceLibrary.Resources.GetString($"PP_Text_{i + 1}")</p>
}

Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater.

I have tried all of the solutions here

  • Got the langversion in web.config
  • Tried surrounding the string with @()
  • Upgraded the project to 4.5.2

and here

  • Changed language version from default to 6
  • Installed CodeDome with Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

I have used string interpolation in other areas of the project (in regular .cs files) without any issue.

What is going on here?

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • 1
    Check Build => Advanced Build Settings. The target framework already updated, but language version may not be updated to use C# 6 yet (https://stackoverflow.com/questions/35686990/feature-interpolated-strings-is-not-available-in-c-sharp-5-please-use-languag). – Tetsuya Yamamoto Sep 20 '17 at 07:27
  • 1
    Which version of VS? – Stuart Sep 20 '17 at 07:37
  • related: https://stackoverflow.com/questions/30832659/string-interpolation-in-a-razor-view – adiga Sep 20 '17 at 07:49
  • perhaps you should install `Microsoft.CodeDom.Providers.DotNetCompilerPlatform` – Sirwan Afifi Sep 20 '17 at 08:37
  • check this [Link](https://stackoverflow.com/questions/30832659/string-interpolation-in-a-razor-view) It helps for the same isssue – Ahmed Yousif Jun 02 '18 at 13:14
  • check this [Link](https://stackoverflow.com/questions/30832659/string-interpolation-in-a-razor-view) It helps for the same issue – Ahmed Yousif Jun 02 '18 at 13:16
  • @Bassie did my answer resolve your issue? – GregH Jun 13 '19 at 13:55

1 Answers1

0

When using C# 5 or older, interpolated strings are not available out of teh box and you will be required to install a NuGet package for this to be supported. Note that (as the error states) this is included out of the box in C# 6 and up.

You'll need to install the NuGet package Microsoft.CodeDom.Providers.DotNetCompilerPlatform to your web project (or whichever project contains your views)

This package will add support for interpolated strings (as well as improve compilation performance). If the package installation did not add the following to your web.config's configuration, then you'll need to add it in manually:

<system.codedom>
<compilers>
  <compiler language="c#;cs;csharp" extension=".cs"
    type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
  <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
    type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
</compilers>
</system.codedom>
GregH
  • 5,125
  • 8
  • 55
  • 109