-->

Code snippet for methods

dev productivity, csharp, visual_studio

Some code snippets are extremely helpful like prop for properties and ctor for constructors. But writing a method is always taking relatively long time as there is no snippet for methods. For good reason I guess as there are all different shapes and colours of methods but I think a snippet can save some time for simple methods. So I decided to create my own snippets. Here’s how to do it in 3 simple steps:

STEP 01: Download the snippet designer from here: http://snippetdesigner.codeplex.com Install and restart visual studio. From File –> New menu select Code Snippet file type.

STEP 02: Save the output of the snippet designer under code snippets folder %USERPROFILE%\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets which looks like this


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>MethodVoid</Title>
      <Author>dummy</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>method_void</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>Method1</ID>
          <ToolTip></ToolTip>
          <Default>
          </Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[public void MyMetHod()
    {
    }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

STEP 03: Restart Visual Studio for the changes to take effect. I created a few snippets for simple methods returning primitive types. Depending on your needs you can choose the optimum number of snippets. I especially like using the test method snippet which looks like this:

TestMethodSnippet

Another great feature about this tool is you can export a selected text block as a snippet. All you have to do is right-click and select Export as Snippet, make the final touches in the editor and save.