C# 8 is fixing interpolated verbatim strings

Ever since string interpolation has been added as a feature in C# there was a minor nuissance that was bugging me quite a bit. If you were using String interpolation on a verbatim string you had to put the symbols in a specific order or else it would not work.

For example the following piece of code would throw a compilation error

var x = @$"Time now at UTC {DateTime.UtcNow}";

And to be specific the exact error it would throw would be

Error	CS1646	Keyword, identifier, or string expected after verbatim specifier: @	

The problem was that @ and $ should be reversed in order for them to be in the correct order. In order to remember the order I was always thinking that @ defines how the characters inside a double quotes are and the $ actually generates the string with the whole set of data, one needs to be executed before the other, thus the specific order. But, in C# 8 both the following would compile successfully.

string x = @$"Time now at UTC {DateTime.UtcNow}";
string s = $@"Time now at UTC {DateTime.UtcNow}";

So my personal nuissance has been eliminated.

Not such a big deal to be honest, but well, if it’s fixed, then why not use it? 🙂

Please follow and like us:

Leave a Reply