Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Visual Studio create double closing brackets for C#6 interpolated strings?

In Visual Studio 2015, when I go to write an interpolated string in a variable by typing an opening curly bracket '{', to achieve the following:

var a = $"{}";

a second ending bracket is automatically inserted, colored red or pink, as so

var a = $"{}}"

After inserting a variable inside of the brackets '{}' and trying to compile, Visual Studio throws an error: "CS8086: A '}' character must be escaped (by doubling) in an interpolated string.". This makes sense, but doesn't at all explain why the second '}' appeared in the first place! I didn't want to write a '}' literal character; I just wanted to write a regular interpolated string - one '}' for one '{'. Every time I go to use interpolated strings I'm forced to manually remove this anomalous second closing bracket in order to compile successfully.

Most curiously, it doesn't appear to happen all the time. If you remove the '{}}' from your interpolated string and type a single '{' again, you will end up with '{}' - just as you would have expected in the first place.

What's up with this behavior?

like image 290
lordmilko Avatar asked Nov 28 '15 12:11

lordmilko


1 Answers

This is being caused by Resharper.

You can prove this by going to

Tools | Options | ReSharper | General | Suspend Now

and then try it again. The extra } will not appear. (I tried with R# 10.0)

I've been working around this problem by pressing ^Z after R# has inserted an extra }, but it's still a bit annoying.

I guess this should be reported as a bug on the R# site (unless it's already been fixed with a recent release).

[EDIT] I've reported it as a bug on the Resharper forums. My bug report should appear there once the mods have approved it.

like image 164
Matthew Watson Avatar answered Oct 21 '22 15:10

Matthew Watson