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?
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";
These prefixes aren't operators. They are only interpreted by the compiler. While it understands $@
it doesn't understand @$
. Why? Because Microsoft's compiler team decided so.
However, support for the latter is planned for C# 8.0
According to msDocs
A verbatim interpolated string starts with the
$
character followed by the@
character.The $ token must appear before the @ token in a verbatim interpolated string.
Perhaps this is the way they designed to be understandable by the current version of c# compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With