简体   繁体   中英

Escaping Quotes inside new C# 6 String Syntax

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 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.

将您的逻辑包装在括号的括号内:

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}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM