问题描述
我要使用逐字字符串,但我误输入的 $
而不是 @
。
I was going to use verbatim string but i mistakenly typed $
instead of @
.
但编译器并没有给我任何的错误和编译成功。
But the Compiler didn't give me any Error and Compiled successfully.
我想知道它是什么,它做什么。我搜索了它,但我无法找到任何东西。
I want to know what it is and what it does. I searched for it but i couldn't find anything.
如何过它不像逐字字符串,因为我不能写:
How ever it is not like verbatim string because i cant write:
string str = $"text\";
有谁知道 $
字符串站之前在C#。
string str = $"text";
我在使用Visual Studio 2015年CTP。
I'm using Visual studio 2015 CTP.
推荐答案
$
短手为的String.Format
,并用于与字符串插值,这是C#6中的新功能作为你的情况下使用,它什么也不做,就像的String.format()
会做什么都没有。
$
is short-hand for String.Format
and is used with string interpolations, which is a new feature of C# 6. As used in your case, it does nothing, just as string.Format()
would do nothing.
用于构建参照其他值的字符串时,它涉及到它自己。什么previously不得不被写成:
It is comes into its own when used to build strings with reference to other values. What previously had to be written as:
var anInt = 1;
var aBool = true;
var aString = "3";
var formated = string.Format("{0},{1},{2}", anInt, aBool, aString);
现在变成了:
var anInt = 1;
var aBool = true;
var aString = "3";
var formated = $"{anInt},{aBool},{aString}";
还有一个选择 - 少为人知 - 使用字符串插值形式 $ @
(这两个符号的顺序很重要)。它允许 @的特点
字符串与 $混合,以支持字符串插值,而不需要为
\\\\
整个字符串。因此,下面两行:
There's also an alternative - less well known - form of string interpolation using $@
(the order of the two symbols is important). It allows the features of a @""
string to be mixed with $""
to support string interpolations without the need for \\
throughout your string. So the following two lines:
var someDir = "a";
Console.WriteLine($@"c:\{someDir}\b\c");
将输出:
c:\a\b\c
这篇关于什么是一个字符串之前$是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!