C # quotes in interpolated string Unexpected character \ 0022

I read that you can use expressions in interpolated strings, but escaping quotes doesn't work.

private string sth = $"{String.Join(\"\", Node.stringToType.Keys)}";


Error CS1056: Unexpected character `\0022' (CS1056)
Error CS1525: Unexpected symbol `)', expecting `${', `:', or `}' (CS1525) 

      

UPDATE:

The inner expression above meant it was equivalent to

String.Join("", Node.stringToType.Keys)

      

(the two backslashes were to escape the two double quotes), as if you could insert some kind of separator in there.

+3


source to share


2 answers


Change this to

private string sth = $"{String.Join("\\", Node.stringToType.Keys)}";

      



This way should work too

private string sth = $"{String.Join(@"\", Node.stringToType.Keys)}";

      

0


source


You must specify it as

  private string sth = $"{String.Join("\\", Node.stringToType.Keys)}";

      

please note that the text inside {...}

must be correct C # code



  String.Join("\\", Node.stringToType.Keys)

      

As a further improvement, you don't need the standard string interpolation:

  private string sth = String.Join("\\", Node.stringToType.Keys);

      

+1


source







All Articles