ARTICLE

Creating Code Snippets in Visual Studio 2010

Posted by Mahadesh Mahalingappa Articles | Visual Studio 2010 September 28, 2011
In this article we will be seeing how we can create Code snippets in Visual Studio 2010.
Reader Level:

In this article we will be seeing how we can create Code snippets in Visual Studio 2010. Code Snippets are very useful and can help us with writing standardized code easily and quickly. 

codeSnpt1.gif

Visual Studio creates an XML file as shown below:

codeSnpt2.gif

Now let's add our snippet.

codeSnpt3.gif

codeSnpt4.gif

codeSnpt5.gif

Let's modify the default snippet generated. I am going to add a snippet here to create a generic class.

I modify the snippet below:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <
Title>title</Title>
    <Author>author</Author>
    <Shortcut>GenericClass</Shortcut>
    <Description>description</Description>
    <SnippetTypes>
      <
SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </
Header>
  <
Snippet>
    <
Declarations>
      <
Literal>
        <
ID>name</ID>
        <Default>value</Default>
      </Literal>
    </
Declarations>
    <
Code Language="Csharp">
      <![CDATA[ 
public class A<T> where T : class
    {

    }
     
]]>
    </
Code>
  </
Snippet>
</
CodeSnippet>

Once modified Save the file as MySnippet.snippet.

codeSnpt6.gif

codeSnpt7.gif

codeSnpt8.gif

codeSnpt9.gif

Hit the Finish Button.

Now let's add a new file and see if the snippet works.

codeSnpt10.gif

You could insert the snippet as shown below :

codeSnpt11.gif

public class A<T> where T : class
{
 
}


Inserting Code snippets can make the code very standardized.
 

Login to add your contents and source code to this article
post comment
     

Hi How to Convert parameter to dynamic?(value type,value name, property name). Tnx Best regard.

Posted by yaquza yaquza Jan 05, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter