Any way to surround code block with Curly Braces {} in VS2008?

I always find myself needing to enclose a block of code in curly braces { }, but unfortunately that isn't included in the C# surround code snippets, which seems to be an oversight. I couldn't find anything on building your own surround snippets either (just other kinds of snippets).

I am actually running Resharper too, but it doesn't seem to have this functionality either (or I haven't figured how to activate it).

We have a coding standard of including even a single line of code after an if or else in curly braces, so if I could just make Resharper do that refactoring automatically that would be even better!


Here is a quick and dirty snippet to do just that.

To Install:

Save the code as SurroundWithBraces.snippet into "<my documents>\Visual Studio Version\Code Snippets\Visual C#\My Code Snippets"

To use:

Select block of text.
Press Ctrl+K, Ctrl+S
Chose My Code Snippets, braces

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>braces</Title>
      <Shortcut>braces</Shortcut>
      <Description>Code snippet to surround a block of code with braces</Description>
      <Author>Igor Zevaka</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp">
        <![CDATA[{
        $selected$ $end$
     }]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

In ReSharper 4.5, curly braces are included as one of the built-in 'Surround Templates':

  1. Select the text that you want curly braces around.
  2. ReSharper -> Code -> Surround With... -> {}

    or

    ALT + R -> C -> S -> 7

    or

    Ctrl+E, U -> 7 (Visual Studio scheme)

    or

    Ctrl+Alt+J -> 7 (ReSharper 2.x/IDEA scheme)


How about:

Ctrl-X, {, Ctrl-V, }

You could even bind that to a macro.


In VS2015 there is an experimental feature that supports it by selecting the text and typing in }.

See here how to enable.