How to use escape characters with string interpolation in C# 6?

后端 未结 7 551
孤街浪徒
孤街浪徒 2020-12-03 06:42

I\'ve been using string interpolation and loving it, however I have an issue where I am trying to include a backslash in my output, but am not able to get it to work.

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 06:45

    $"{domain}\\{user}"
    

    Works fine - escaping works as usual (except when escaping {). At least on .NET 4.6 and VS 14.0.22823 D14REL.

    If it doesn't work for some reason (maybe you're using an older version of the compiler?), you could also try being more explicit:

    $"{domain}{@"\"}{user}"
    

提交回复
热议问题