Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating Custom C# Code Snippets in Visual Studio

0.00/5 (No votes)
2 Dec 2015 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 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.

<![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 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here