Back to all posts

Visual Studio Code Snippets - Info Object and Class Properties

Posted on Mar 02, 2008

Posted in category:
Development
C#

Recently I have been asked by multiple people what rapid design tools I use, or what types of code generation tools do I use.  Many people are surprised when I tell them that for the most part, I don't use any rapid generation tools.  I do use a few homegrown tools, such as my Info Class Generator, which is available on this site.  However, that is as far as I go with automated code generators.  However, one thing that I have started using more and more frequently are Code Snippets within Visual Studio.

I have found that creating a few helpful Code Snippets has allowed me to quickly and easily format my code, and build my needed structures in almost no time.  In this blog article, I will share two of my most commonly used Code Snippets, if there is a demand I will post future code snippets here as well.

Info Class Snippet

One of the most common actions that I perform on a regular basis is the creation of information object classes.  These are classes that have three basic sections to them; private data members, constructors, and public properties.  This snippet is a very basic snippet that just structures a class for me with the standard regions already setup, I just have to drop in the specifics and am ready to go.  The shortcut for this snippet is "ioclass" and if you type that followed by a tab Visual Studio will insert the following code.

Complete Info Class Snippet
///<summary>
///
///</summary>
public class MyClass
{
    #region Private Data Members

    #endregion

    #region Public Constructors
    ///<summary>
    /// Default code constructor
    ///</summary>
    public MyClass()
    {
        //Initalize any needed values

    }
    #endregion

    #region Public Properties

    #endregion
}

I find this very helpful as I can create classes with the exact same regions every time.

C# Property Bodies

The second snippet that I use on a regular basis is a snippet that will automatically form the body of a C# public property, I have 3 autofill regions setup in the snippet, the first for the data type, the second for the public name, and the third for the private member's name.  This allows me to create a public get and set property without typing a single curly brace or return key.  Typically under 10 keystrokes.  The default text of this snippet is below, the access key for this snippet is pbody.

Property Body Snippet
/// <summary>
///
/// </summary>
public int YourMember
{
    get { return _yourMember; }
    set { _yourMember = value; }
}

This is another basic snippet but for those that use C# on a regular basis, you will know just how helpful this code is.

Please share any comments below.