Click here to Skip to main content
15,894,405 members
Articles / Programming Languages / C#
Tip/Trick

Creating Custom C# Code Snippets in Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.06/5 (16 votes)
2 Dec 2015CPOL2 min read 22.7K   162   14   2
Code snippets are automatic code generators that can generate code with just a unique string and a tab press.

Introduction

This tip is aimed at creating custom code snippets in Visual Studio. The example shown applies to C# but can be extended to any language supported by Visual Studio.

Background

A bit of XML is preferred, but don't worry if you don't know how - it's not hard at all. (I don't know even a bit of XML!)

Using the Code

Snippets can be written in XML and can be made for any language in Visual Studio. Below is an example for code snippet for a property changed event handler method.

XML
<?xml version="1.0" encoding="utf-8"?>
<codesnippet format="1.0.0" 
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Notify Property Changed Method</Title>
    <author>Akash</author>
    <shortcut>npcm</shortcut>
    <description>This method implements the 
    OnPropertyChanged method and binds to the event handler</description>
    <snippettypes>
      <snippettype>SurroundsWith</snippettype>
      <snippettype>Expansion</snippettype>
    </snippettypes>
  </Header>
  <snippet>

    <code language="CSharp">
      <![CDATA[#region Notify Property Changed Members
  public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if(handler!=null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }    
        #endregion]]>
    </code>
  </snippet>
</codesnippet>

In the <code> block, you can specify the language you are creating the snippet for.

In the Header <shortcut> tag defines what you need to type in the editor to activate the snippet.

All the code goes between the tags.

XML
<![CDATA[ "Your Code here" ]]>

This is the snippet for a Notifiable Property.

It has some differences when compared to the above one. This snippet allows you to enter custom data wherever necessary. For example, in the below snippet 'Type' and 'Property' can be replaced when the snippet is activated.

The beauty of this is that you only need to change any string once and it changes in all parts of your snippet.

XML
<?xml version="1.0" encoding="utf-8"?>
<codesnippet format="1.0.0" 
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Notifiable Property</Title>
    <author>Akash</author>
    <shortcut>nprop</shortcut>
    <description>Property With in Built Property Changed method implementation.</description>
    <snippettypes>
      <snippettype>SurroundsWith</snippettype>
      <snippettype>Expansion</snippettype>
    </snippettypes>
  </Header>
  <snippet>
    <declarations>
      <literal>
        <id>Type</id>
        <default>string</default>
      </literal>
      <literal>
        <id>Property</id>
        <default>PlaceHolder</default>
      </literal>
    </declarations>
    <code language="CSharp">
      <![CDATA[private $Type$ _$Property$;
        public $Type$ $Property$
        {
            get { return _$Property$; }
            set { 
               if(value!=null || value != _$Property$) _$Property$ = value;
               OnPropertyChanged("$Property$");
            }
        }]]>
    </code>
  </snippet>
</codesnippet>

Final Steps

  1. Save the file with .snippet extension.
  2. Save it in C:\Users\{User Name}\Documents\Visual Studio {Version}\Code Snippets\{language}\.
  3. Restart the VS environment.
  4. Have fun using the shortcut using the "Shortcut " tag... Just remember you have your friend intellisense.

Points of Interest

This will accelerate your coding speed and if you are a serious developer, then it will clearly show effect on your projects progress speed.

History

  • 2nd December, 2015: Initial version

I've uploaded my C# snippets in rar format, they are for MVVM. Feel free to use them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIn VS2015 the xml tag names are case sensitive Pin
djmarcus6-Dec-15 6:32
djmarcus6-Dec-15 6:32 
GeneralMy vote of 4 Pin
Santhakumar M5-Dec-15 19:11
professionalSanthakumar M5-Dec-15 19:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.