2

Is there a way to highlight a piece of code in Visual Studio C# and automatically place a pair of parentheses around the block of code rather than manually put your cursor at the beginning and press '(' and then the end and press ')'?

I've used other IDEs that have had this feature and as simple as it sounds, it's very nice to have.

I'm currently working with Visual Studio 2017.

user4157124
  • 2,809
  • 13
  • 27
  • 42
Jee
  • 969
  • 1
  • 7
  • 26
  • 1
    You can create a snippet, here's one that adds braces around the selected code, it should be easy enough to replace the braces with your parentheses ... https://stackoverflow.com/questions/2769508/any-way-to-surround-code-block-with-curly-braces-in-vs2008/2769697#2769697 ... and here's how you can use the snippet... https://learn.microsoft.com/en-us/visualstudio/ide/how-to-use-surround-with-code-snippets?view=vs-2019 – quaabaam Sep 04 '20 at 18:44

1 Answers1

3

To my knowledge, Visual Studio doesn't provide that functionality out of the box. However, there are free extensions you can download from the Visual Studio Marketplace that do what you describe such as:

You can download them from the marketplace using the links above or by searching in visual studio (Menu: Extensions -> Manage Extensions -> perform your search) Disclaimer I’ve only tried out the first one of these briefly and it appears to work. I will update after checking the second one.

Edit: I've now tried both and both worked. Please note that I use VS2019 so I can't vouch for their performance on VS2017. Of the two options I mentioned, Surround Selection claims to work with VS2017 while the second one only lists VS2019.

CodifiedChaos
  • 55
  • 1
  • 6