Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $ as a prefix when declaring a string

Tags:

c#

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.

like image 625
Lode Vlaeminck Avatar asked Feb 22 '16 15:02

Lode Vlaeminck


2 Answers

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.

like image 55
danludwig Avatar answered Nov 08 '22 01:11

danludwig


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.

like image 35
Mihai Avatar answered Nov 08 '22 00:11

Mihai