Posts Tagged ‘snippets’

Visual studio snippet for an enhanced #region in csharp

Mar 2nd, 2013

Visual studio contains snippets.  These are “shortcuts” for writing various code.  Try writing for in the editor and text is typed in for you and you get placeholders for faster code writing.

One can write code like this oneself.  It is quite easy.  Just go to menu->Tools->Code snippets manager and work from there; copy, paste and rewrite to your will.  (The location field is the folder of the very snippets.)

Here is an example of mine.

I know that some people swear that #region is the devil’s own child but there are other, let’s call us them for pragmatists for now that say that if it fits it fits.  The standard snippet outcome for a region is

1
2
3
4
5
#region // Name of region.
//code
//code
//code
#endregion

but I prefer

1
2
3
4
5
#region // Name of region.
//code
//code
//code
#endregion // Name of region.

So I just copypasted the existing snippet to this; which you can import through above mentioned menu.<?xml version=”1.0″ encoding=”utf-8″ ?>

<CodeSnippets xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>
<CodeSnippet Format=”1.0.0″>
<Header>
<Title>#region</Title>
<Shortcut>region</Shortcut>
<Description>Code snippet for #region</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>name</ID>
<ToolTip>Region name</ToolTip>
<Default>MyRegion</Default>
</Literal>
</Declarations>
<Code Language=”csharp”><![CDATA[#region $name$
$selected$ $end$
#endregion // $name$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>