Home > front end >  String interpolation cannot be recognized in visual studio when .net framework is set to 4.6.2
String interpolation cannot be recognized in visual studio when .net framework is set to 4.6.2

Time:11-01

It is an asp.net website project with .net framework 4.6.2, and the C# language level is set to 6.0. While developing code in Visual Studio 2022, below code cannot be recognized.

Console.WriteLine($"Hello {name}");

Since string interpolation is a new feature introduced into C# 6, and the language level in our project is alreay set into C# 6, why is still not recognized by the syntax?

After, I run the command in Nuget console with Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 3.6.0, the problem is gone.

Why is it not working with C# 6 previously? What does DotNetCompilerPlatform do to make it work?

CodePudding user response:

Finally I get it, and share it with you who may also encouter the same issue.

.Net Framework 4.6.2 already supports C# 6 syntax, including string interpolation.

If it is a console application, by default, it will use the compier(Roslyn) of .Net Framework 4.6.2, who already supports C# 6. Everything is fine. But if it is a asp.net website project, because the C# code is not compiled in VisualStudio, instead, it will be compiled in IIS when it receives the first http request. By default, IIS will not use Roslyn to compile the website. So, we need to tell IIS to use the specific compiler to support C#6. This is why we have to run below command

Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 3.6.0

While we are writing asp.net website C# code in Viusal Studio, it may also have similar mechanism so it shows red wave line to tell there is error.

  • Related