[英]Escaping Quotes inside new C# 6 String Syntax
I'm really excited about the new features in C# 6, including the new string syntax: 我对C#6的新功能感到非常兴奋,包括新的字符串语法:
var fullName = $"My Name is {FirstName} {LastName}";
However, I can't figure out how to escape quotes inside the braces to do the follow: 但是,我不知道如何在括号内转义引号来执行以下操作:
bool includePrefix = true;
var fullName = $"My name is {includePrefix ? "Mr. " : ""}{FirstName} {LastName}";
C# 6 doesn't like that. C#6不喜欢这样。 I've had to revert to using
String.Format
in that second case. 在第二种情况下,我不得不恢复使用
String.Format
。 Is it possible to escape quotes using the new syntax? 是否可以使用新语法对引号进行转义?
Update: Yes, I have tried using the \\
escape, but it's not recognized. 更新:是的,我尝试使用
\\
转义符,但无法识别。
将您的逻辑包装在括号内的括号内:
var fullName = $"My name is {(includePrefix ? "Mr. " : "")}{FirstName} {LastName}";
Regularly to escape quotes you need to use a slash (ie \\"
). 定期转义引号需要使用斜杠(即
\\"
)。
However, this is not the issue here, as you don't need to escape, you're just missing parentheses over the expression. 但是,这不是这里的问题,因为您不需要转义,只是在表达式上缺少括号。
This works: 这有效:
bool includePrefix = true;
var fullName = $"My name is {(includePrefix ? "Mr. " : "")}{FirstName} {LastName}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.