Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 647
Robert Synoradzki Avatar asked Jan 31 '19 10:01

Robert Synoradzki


Video Answer


3 Answers

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";
like image 64
DavidG Avatar answered Oct 19 '22 10:10

DavidG


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

like image 37
Bill Tür stands with Ukraine Avatar answered Oct 19 '22 11:10

Bill Tür stands with Ukraine


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.

like image 29
Tasnim Fabiha Avatar answered Oct 19 '22 12:10

Tasnim Fabiha