Search code examples
c#stringprefixstring-interpolation

Why can't I use @$ prefix before strings?


All these string prefixes are legal to use in C#:

  "text"
 @"text"
 $"text"
$@"text"

Why isn't this?

@$"text"

One would have thought that the order of these operators doesn't matter, because they have no other meaning in C# but to prefix strings. I cannot think of a situation when this inverted double prefix would not compile. Is the order enforced only for aesthetic purposes?


Solution

  • Interpolated verbatim strings were not allowed before C# version 8, for no other reason than they weren't implemented. However, this is now possible, so both of these lines will work:

    var string1 = $@"text";
    var string2 = @$"text";