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.


wrap your logic inside parentheses, inside the brackets:

var fullName = $"My name is {(includePrefix ? "Mr. " : "")}{FirstName} {LastName}";

Regularly to escape quotes you need to use a slash (i.e. \").

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