Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation doesn't work with .NET Framework 4.6

I just installed the .NET Framework 4.6 on my machine and then created a ConsoleApplication targeting .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?

like image 665
Bidou Avatar asked Jul 20 '15 11:07

Bidou


People also ask

How do you interpolate a string in C#?

An interpolated verbatim string starts with the $ character followed by the @ character. Starting with C# 8.0, you can use the $ and @ tokens in any order: both $@"..." and @$"..." are valid interpolated verbatim strings. To include a brace, "{" or "}", in a result string, use two braces, "{{" or "}}".

Can I use string interpolation?

Beginning with C# 10, you can use string interpolation to initialize a constant string. All expressions used for placeholders must be constant strings. In other words, every interpolation expression must be a string, and it must be a compile time constant.

What does string interpolation do?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

What is variable interpolation in C#?

It is also known as variable substitution, variable interpolation, or variable expansion. It is a process of evaluating string literals containing one or more placeholders that get replaced by corresponding values.


2 Answers

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

like image 91
Daniel Hilgarth Avatar answered Oct 08 '22 16:10

Daniel Hilgarth


String interpolation is indeed a C# 6.0 feature, but C# 6 isn't limited to VS2015.

You can compile applications that leverage C# 6.0 language features in VS2013 by targeting the Roslyn compiler platform via the Microsoft.Net.Compilers NuGet package.

My experience has been that, after this package is installed, error messages during compilation can be a little misleading. If you have compile errors that are not C# 6 related, you will be shown those error messages plus error messages regarding invalid syntax relating to any C# 6 features you've used despite the fact that you're now targeting a compiler that supports them.

For instance...

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

will result in 4 error messages during compilation:

Error 1 Unexpected character '$'

Error 2 Invalid expression term ''

Error 3 ; expected

Error 4 ; expected

The first 3 errors here relate to the line that uses string interpolation, only the last ; expected error is a problem. Remove the offending line right before we return the View and the string interpolation compile errors disappear and all is well.

like image 34
joelmdev Avatar answered Oct 08 '22 18:10

joelmdev