How to escape string interpolation in snippet
Asked Answered
P

1

8

String interpolation is great and snippets are great, but they don't play nice together. I have a snippet that looks (in part) like this:

job.Location = $"{e["$locationfield$"]}";
return true;

The locationfield part is supposed to be the replacement, but of course it will see as '"{e[" being a replacement. So when you try to use the snippet, that part is messed up:

job.Location = locationfield

And the rest of the code following is gone (because it can't match another $ presumably).

Is there a way around that? Or can you just not use string interpolation in your snippets?

Pianoforte answered 30/8, 2016 at 20:51 Comment(0)
P
8

Ok, as it turns out, it's dead simple. I did this on a whim and it worked:

job.Location = $$"{e["$locationfield$"]}";
return true;

Another solution that I didn't try, is that you can actually specify what the snippet should use as a delimiter:

<Code Language="csharp" Delimiter="$">

Just change the $ to something else you aren't using in this particular block of code.

Pianoforte answered 30/8, 2016 at 20:55 Comment(1)
@OlivierJacot-Descombes: I know, but not for 2 days. Which means I'll probably forget it altogether.Pianoforte

© 2022 - 2024 — McMap. All rights reserved.