在新的C#6字符串语法中转义引号 [英] Escaping Quotes inside new C# 6 String Syntax
本文介绍了在新的C#6字符串语法中转义引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对C#6的新功能感到非常兴奋,包括新的字符串语法:
I'm really excited about the new features in C# 6, including the new string syntax:
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不喜欢这样.在第二种情况下,我不得不恢复为使用 String.Format
.是否可以使用新语法对引号进行转义?
C# 6 doesn't like that. I've had to revert to using String.Format
in that second case. Is it possible to escape quotes using the new syntax?
更新:是的,我尝试使用 \
转义符,但无法识别.
Update: Yes, I have tried using the \
escape, but it's not recognized.
推荐答案
将您的逻辑放在括号内,在括号内
wrap your logic inside parentheses, inside the brackets:
var fullName = $"My name is {(includePrefix ? "Mr. " : "")}{FirstName} {LastName}";
这篇关于在新的C#6字符串语法中转义引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文