In MVC 6 source code I saw some code lines that has strings leading with $ signs.
在MVC 6源代码中,我看到一些代码行,其中包含带有$符号的字符串。
As I never saw it before, I think it is new in C# 6.0. I'm not sure. (I hope I'm right, otherwise I'd be shocked as I never crossed it before.
正如我之前从未见过的那样,我认为它是C#6.0中的新功能。我不确定。 (我希望我是对的,否则我会感到震惊,因为我从来没有碰过它。
It was like:
它就像:
var path = $"'{pathRelative}'";
1 个解决方案
#1
You're correct, this is a new C# 6 feature.
你是对的,这是一个新的C#6功能。
The $
sign before a string enables string interpolation. The compiler will parse the string specially, and any expressions inside curly braces will be evaluated and inserted into the string in place.
字符串前面的$符号启用字符串插值。编译器将特别解析字符串,并且将评估花括号内的任何表达式并将其插入到字符串中。
Under the hood it converts to the same thing as this:
在引擎盖下,它转换为与此相同的东西:
var path = string.Format("'{0}'", pathRelative);
Let's look at the IL for this snippet:
让我们看一下这段代码的IL:
var test = "1";
var val1 = $"{test}";
var val2 = string.Format("{0}", test);
Which compiles to:
编译为:
// var test = "1";
IL_0001: ldstr "1"
IL_0006: stloc.0
// var val1 = $"{test}";
IL_0007: ldstr "{0}"
IL_000c: ldloc.0
IL_000d: call string [mscorlib]System.String::Format(string, object)
IL_0012: stloc.1
// var val2 = string.Format("{0}", test);
IL_0013: ldstr "{0}"
IL_0018: ldloc.0
IL_0019: call string [mscorlib]System.String::Format(string, object)
IL_001e: stloc.2
So the two are identical in the compiled application.
所以这两个在编译的应用程序中是相同的。
A note on the C# string interpolation syntax: Unfortunately the waters are muddied right now on string interpolation because the original C# 6 preview had a different syntax that got a lot of attention on blogs early on. You'll still see a lot of references to using backslashes for string interpolation, but this is no longer syntactically valid.
关于C#字符串插值语法的注释:不幸的是,现在对于字符串插值,水域变得混乱,因为原始的C#6预览具有不同的语法,早期在博客上得到了很多关注。你仍然会看到许多使用反斜杠进行字符串插值的引用,但这在语法上不再有效。
#1
You're correct, this is a new C# 6 feature.
你是对的,这是一个新的C#6功能。
The $
sign before a string enables string interpolation. The compiler will parse the string specially, and any expressions inside curly braces will be evaluated and inserted into the string in place.
字符串前面的$符号启用字符串插值。编译器将特别解析字符串,并且将评估花括号内的任何表达式并将其插入到字符串中。
Under the hood it converts to the same thing as this:
在引擎盖下,它转换为与此相同的东西:
var path = string.Format("'{0}'", pathRelative);
Let's look at the IL for this snippet:
让我们看一下这段代码的IL:
var test = "1";
var val1 = $"{test}";
var val2 = string.Format("{0}", test);
Which compiles to:
编译为:
// var test = "1";
IL_0001: ldstr "1"
IL_0006: stloc.0
// var val1 = $"{test}";
IL_0007: ldstr "{0}"
IL_000c: ldloc.0
IL_000d: call string [mscorlib]System.String::Format(string, object)
IL_0012: stloc.1
// var val2 = string.Format("{0}", test);
IL_0013: ldstr "{0}"
IL_0018: ldloc.0
IL_0019: call string [mscorlib]System.String::Format(string, object)
IL_001e: stloc.2
So the two are identical in the compiled application.
所以这两个在编译的应用程序中是相同的。
A note on the C# string interpolation syntax: Unfortunately the waters are muddied right now on string interpolation because the original C# 6 preview had a different syntax that got a lot of attention on blogs early on. You'll still see a lot of references to using backslashes for string interpolation, but this is no longer syntactically valid.
关于C#字符串插值语法的注释:不幸的是,现在对于字符串插值,水域变得混乱,因为原始的C#6预览具有不同的语法,早期在博客上得到了很多关注。你仍然会看到许多使用反斜杠进行字符串插值的引用,但这在语法上不再有效。