Friday, May 4, 2012

How to Create C# Code Snippet in Visual Studio 2010

Visual studio provides some inbuilt IntelliSense Code Snippets for Expansion, SurroundsWith and Refactoring.


When you have some piece of code and you need to use each and every file, instead of rewriting you can create you own code snippet.

that will saves your times to rewriting block of code.

IntelliSense Code Snippets created in XML file.

There are Three Snippet types available.

   1. Expansion
   2. SurroundsWith 
   3. Refactoring

In this post I am going to create Expansion Code Snippet for Property block with MVVM functionality that is RaisePropertyChanged.

Step 1 : First Create XML file in Visual Studio 2010 and save file as propmvvm.snippet.


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

Code Snippet file start with CodeSnippets element which has xmlns attribute for snippet.


Step 2 : Add CodeSnippet Section under root(CodeSnippets) section
 <?xml version="1.0" encoding="utf-8"?>
 <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">

Step 3 : Add Header Section within CodeSnippet Section.
    <Header>
      <Title>propmvvm</Title>
      <Shortcut>propmvvm</Shortcut>
      <Description>
        Code snippet for property with raisepropertychanged event
      </Description>
      <Author>Microsoft</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>


This section has elements like Title, shortcut for IntelliSense and Description.

when you type using IntelliSens it will shows list of snippet including which you have created with given shortcut.

Description element shows detailed description of snippet on MouseOver of you snippet.

SnippetType  describes which type of snippet are you going to create like Expansion(prop) or SurroundWith (try/catch).

Step 4 : Add Snippet section and add Declaration Section within Snippet tag.

<Snippet>
   <Declarations> 
   ......
   ......
   </Declaration>
<Snippet> 
      

Step 5 : Create Literals or Objects within Declaration Section.
<Declarations> 
   <Literal>
          <ID>type</ID>
          <ToolTip>Property Type</ToolTip>
          <Default>int</Default>
   </Literal>
   <Literal>
          <ID>property</ID>
          <ToolTip>Property Name</ToolTip>
          <Default>MyProperty</Default>
   </Literal>
   <Literal>
          <ID>field</ID>
          <ToolTip>The variable baking this property</ToolTip>
          <Default>MyVariable</Default>
   </Literal>
</Declaration>
 

Literal Element are editable values that are inserted by user into snippet.
In above code I have created Three Literals.

First Literal describes Type of property or variable, i have set Default value to int ( you can give any datatypes including (double, bool ,string etc.)

Second Literal element is for editable name of Property.

Third Literal element has field, user can gives any variable name that used within property Get/Set.

Step 6 : Create Code Element within Snippet section, Inside the Code element, add the C# code for the snippet.

   <Code Language="CSharp">
        <![CDATA[private $type$ $field$;

 public $type$ $property$
 {
  get { return $field$;}
  set { $field$ = value;
    RaisePropertyChanged("$property$");
    }
 }
 $end$]]>
 

This section describes actual code in C# for snippet,
In code section first you have to set Language in which you are going to create snippet.
In this section we are using Declaration Literals  type,fields and property name.

Below is  Full source Code of Property block Snippet

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propmvvm</Title>
      <Shortcut>propmvvm</Shortcut>
      <Description>Code snippet for property with raisepropertychanged event</Description>
      <Author>Microsoft</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>type</ID>
          <ToolTip>Property Type</ToolTip>
          <Default>int</Default>
        </Literal>
        <Literal>
          <ID>property</ID>
          <ToolTip>Property Name</ToolTip>
          <Default>MyProperty</Default>
        </Literal>
        <Literal>
          <ID>field</ID>
          <ToolTip>The variable baking this property</ToolTip>
          <Default>MyVariable</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[private $type$ $field$;

 public $type$ $property$
 {
  get { return $field$;}
  set { $field$ = value;
    RaisePropertyChanged("$property$");
    }
 }
 $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Your Snippet is created. no how to import snippet in Visual Studio.

Import Snippet in Visual Studio.

Step 1 : Go to Tools Menu and click on Code Snippets Manager item.


it will open Code Snippets Manager dialog as shown in below image.




 Step 2 : Click on Import.. Button and select your snippet file, it will ask for selection location for snippets as below screen.


Select location for snippets and click on Finish button, your code snippet imported successfully.
Now you can use your snippet in development.

Using Snippets in C# 
Create class file in your project.
In that file start typing with you snippet shortcut.
it will display  list of IntelliSense code snippets including your snippet




 As shown in above image, you have list of snippets with your created snippet.
when select snippet as you created it gives popup with description of snippet as you have written in Header section of snippet XML file.
Now Select you snippet it will insert block of code as in blow image.




 Now change type which you want for property it will automatically change type all the places in you block.
same as when you change variable name , it will automatically change variable in Get/Set of property block.
In this way you can create you own snippets.


Conclusion
So by using snippets you saves lots of time of rewriting piece of code. 



No comments:

Post a Comment