Region code snippet

To organize my code, I use a lot of regions to make it more readable. I love regions. Unfortunately, Visual Studio doesn’t provide a default code snippet to define regions. So I created one for myself. I’m publishing the code because I often change from PC or because of reinstalls etc. The internet is always (or most of the time) available, so I can easily download my snippet from here.

The code snippet

Copy the XML to a file and name it “region.snippet”


<CodeSnippets xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>
<CodeSnippet Format=”1.0.0″>
<Header>
<Shortcut>region</Shortcut>
<Description>Code snippet for an automatically creating a region</Description>
<Author>Mike Bevers</Author>
<SnippetTypes>
<SnippetType>SurroundsWith</SnippetType>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>RegionName</ID>
<ToolTip>Region Name</ToolTip>
<Default>MyRegion</Default>
</Literal>
</Declarations>
<Code Language=”CSharp”>
<![CDATA[#region $RegionName$
$selected$ $end$
#endregion]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


 

Installing a code snippet

There are two ways to install a code snippet.

First, you can install a snippet by coping it to the Visual Studio User directory, mostly found under “My Documents\Visual Studio 200x\Code Snippets\Visual C#\My Code Snippets”.

Second, you can use the Visual Studio Code Snippets Manager, found under “Tools => Code Snippets Manager”. Just highlight “My Code Snippets”, click the Import button and browse to your custom snippet.

In some cases, the snippet manager mysteriously disappears from the menu bar. You can add the command back by going into Tools/Customize, Click on the Commands Tab, select the Tools Menu, and drag the Code Snippets Manager onto any commandbar that you like.

Want to know more about code snippets?

The msdn site is the place to be!