Click here to Skip to main content
13,191,400 members (71,783 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.4K views
114 downloads
14 bookmarked
Posted 2 Dec 2015

Creating Custom C# Code Snippets in Visual Studio

, 2 Dec 2015
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Akash Gutha
Student
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionIn VS2015 the xml tag names are case sensitive Pin
djmarcus6-Dec-15 6:32
memberdjmarcus6-Dec-15 6:32 
GeneralMy vote of 4 Pin
Santhk5-Dec-15 19:11
professionalSanthk5-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171017.1 | Last Updated 2 Dec 2015
Article Copyright 2015 by Akash Gutha
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid