String interpolation does not work with .NET Framework 4.6

I just installed the .NET Framework 4.6 on my computer and then created the ConsoleApplication application with the .NET Framework 4.6 with Visual Studio 2013.

I wrote the following in the Main method:

  string test = "Hello"; string format = $"{test} world!"; 

But this does not compile. Doing the same in Visual Studio 2015 works.
Why?

+25
c # visual-studio string-interpolation
Jul 20 '15 at 11:00
source share
2 answers

String interpolation is a C # 6.0 feature, not one of the .NET Framework 4.6. VS 2013 does not support C # 6, but VS 2015 does.

+42
Jul 20 '15 at 11:01
source share

String interpolation is indeed a feature of C # 6.0, but C # 6 is not limited to VS2015.

You can compile applications that use the features of C # 6.0 in VS2013 by targeting the Roslyn compiler platform through the Microsoft.Net.Compilers NuGet package.

My experience is that after installing this package, error messages during compilation may be a little misleading. If you have compilation errors that are not related to C # 6, you will be shown these error messages plus error messages regarding invalid syntax related to any C # 6 functions that you used, despite the fact that now you are targeting a compiler that supports them.

For example...

 public class HomeController : Controller { public ActionResult Index() { ViewBag.Title = "Home Page"; var example = $"{ViewBag.Title}"; ImASyntaxErrorWhatAmIWheresMySemicolonLOL return View(); } } 

will result in 4 compilation error messages:

Error 1 Unexpected '$' character

Error 2 Invalid expression term ''

Error 3; Expected,

Error 4; Expected,

The first 3 errors here relate to a line that uses line interpolation, only the last error ; expected ; expected is a problem. Delete the violation line right before we return the View , and the compilation errors of the interpolation line will disappear, and everything will be fine.

+25
Oct 16 '15 at 20:49
source share



All Articles