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

3

5

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?

Pressurize answered 31/1, 2019 at 10:16 Comment(1)
I don't think they are commutative: $@"" means interpolate in a verbatim string, that makes sense. But what would you have @$"" do? Take the string verbatim after interpolation ?Windowlight
C
9

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";
Coloratura answered 31/1, 2019 at 10:18 Comment(0)
P
7

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

Paratuberculosis answered 31/1, 2019 at 10:19 Comment(0)
I
1

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.

Inexcusable answered 31/1, 2019 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.