8

I cannot compile the following code

var baseUrl = $"http://{endPoint}/";

I got this code from a github project, and i guess the $-sign tells the compiler to use the value of the attribute(and even do methods that return a string).

I have been looking online, but i cant seem to find anything but @"String" to ignore escape characters.

Lode Vlaeminck
  • 894
  • 1
  • 9
  • 24
  • 1
    The `$` is for string interpolation, and is a C#6 feature. https://msdn.microsoft.com/en-us/library/dn961160.aspx – danludwig Feb 22 '16 at 15:16
  • 1
    What version of Visual Studio are you using? You'll need to be using VS 2015 for C# 6. – Tim Feb 22 '16 at 15:18
  • Note that if you wish to use C# 6 [you need Visual Studio 2015, or an extension on Visual Studio 2013](http://stackoverflow.com/q/27093908/993547). – Patrick Hofman Feb 22 '16 at 15:24

2 Answers2

9

The $ does string interpolation, which is a C# 6 feature.

It's the equivalent of doing this:

var baseUrl = string.Format("http://{0}/", endPoint);

You can read more about it here on MSDN.

If it does not compile for you, it's probably because you are using a version of Visual Studio that does not support C#6 features.

danludwig
  • 46,965
  • 25
  • 159
  • 237
  • Oké, I did not know the term 'interpolation' making googling quite ineffective :). I am working in an older version of visual studio. – Lode Vlaeminck Feb 22 '16 at 15:30
1

The $ string prefix is only available in C# 6. To change your targeted C# version in Visual Studio, go to your project properties → BuildAdvanced...Language Version, and select C# 6.0 .

Note that C# 6 is only supported in VS 2015 by default.

Mihai
  • 228
  • 1
  • 9