在C#中,三重双引号"""
的目的是什么?它似乎用于多行文本。但为什么不使用带有@"..."
的单个双引号呢?
string text = """
some text
some text
some text
""";
我认为一个简单的例子可以比许多文字更好地解释。 假设我们有一个 SQL 查询,我们想要保持良好的格式以便于阅读。
如果我们过于简单粗暴,它将无法编译:
string sql =
"select id,
name
from MyTable"; // <- Doesn't compile
@
来编译verbatim字符串。string sql =
@"select id,
name
from MyTable";
...
// A little bit different format somewhere else in c# code
string sameSql = @"select id,
name
from MyTable";
从SQL开始:
select id,
name
from MyTable
从 sameSql
中,我们得到了相同的查询语句,但格式不同:
select id,
name
from MyTable
string sql =
"""
select id,
name
from MyTable
""";
...
// A little bit different format
string sameSql = """
select id,
name
from MyTable
""";
select id,
name
from MyTable
查询将被解析、优化并仅缓存一次,C#代码风格被忽略。
If you work with strings literal that contain quotes or embedded language strings like JSON, XML, HTML, SQL, Regex and others, raw literal strings may be your favorite feature of C# 11. Previously if you copied a literal string with quotes into a C# literal, the string ended at the first double quote with compiler errors until you escaped each one. Similarly, if you copied text with curly braces into an interpolated string literal, each curly bracket was interpreted as the beginning of a nested code expression unless you escape it, generally by doubling the curly bracket.
Raw string literals have no escaping. For example, a backslash is output as a backslash, and
\t
is output as the backslash and at
, not as the tab character.Raw string literals start and end with at least three double quotes (
"""..."""
). Within these double quotes, single"
are considered content and included in the string. Any number of double quotes less than the number that opened the raw string literal are treated as content. So, in the common case of three double quotes opening the raw string literals, two double quotes appearing together would just be content. If you need to output a sequence of three or more double quotes, just open and close the raw string literal with at least one more quote than that sequence.Raw string literals can be interpolated by preceding them with a
$
. The number of$
that prefixes the string is the number of curly brackets that are required to indicate a nested code expression. This means that a$
behaves like the existing string interpolation – a single set of curly brackets indicate nested code. If a raw string literal is prefixed with$$
, a single curly bracket is treated as content and it takes two curly brackets to indicate nested code. Just like with quotes, you can add more$
to allow more curly brackets to be treated as content. For example:
const int veryCold = -30; const int comfortable = 20; string jsonString = $$""" { "TemperatureRanges": { "Cold": { "High": {{comfortable}}, "Low": {{veryCold}} } } } """;
Raw string literals also have new behavior around automatically determining indentation of the content based on leading whitespace. To learn more about this and to see more examples on this feature, check out the docs article Raw String Literals.
P.S. 感谢Roe和ProgrammingLlama指出本文。
"""
的原因。 - freakish