无需使用转义序列。对于'在 C# 中?
在 MSDN 上,我可以读到 \'
是 '
字符的转义序列。但是我可以在没有转义序列的字符串中使用它,如下所示:
Console.WriteLine("Press 'X' ");
怎么可能?
On MSDN I can read that \'
is escape sequence for '
char. However I am able to use it in string without escape sequence like this:
Console.WriteLine("Press 'X' ");
How it is possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
但是如何将其写为
char
呢?But how would you write it as a
char
?char
< /a> (单个字符文字)是与string
(多字符文字)。在 C# 中,
char
声明为:而
string
声明为:如您所见,需要使用单引号 (
'
)转义以声明包含单引号的char
:char
(a single character literal) is a different data type thanstring
(a multi character literal).In C# a
char
is declared as:whereas a
string
is declared as:As you can see the single quote (
'
) would need to be escaped to declare achar
containing the single quote:字符文字需要
\'
筛选。原因是'
可以解释为文字边界字符。对于字符串来说这是没有意义的,因为没有什么可以混淆的。在strings
中,\"
又有意义。\'
screening is needed for char literals. Reason is that'
can be interpreted as literal boundary character. For strings it is meaningless because there is nothing to confuse with. Instrings
in turn\"
makes sense.它表示您必须对 char 数据类型进行转义 ' 。
It says that you have to escape ' for a char data type.