BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Visual Studio
VS 2005+

Visual Studio Property Code Snippet

Visual Studio 2005 includes some helpful shortcuts in the form of code snippets. The property snippet allows a property to be quickly added to a class with a few keystrokes. However, the results vary between versions of the development environment.

Code Snippets

Code snippets were introduced in Visual Studio 2005. They allow a short name, or alias, to be typed. This alias is automatically replaced with a larger section of standard code. Using Intellisense code snippets, the integrated development environment (IDE) assists further by highlighting key areas of the inserted code that may require modification.

The Property Snippet

The property snippet is used to generate the code for a property declaration within a class. In Visual Studio 2005, typing the text "prop", followed by two presses of the tab key generates the following standard property declaration:

private int myVar;

public int MyProperty
{
    get { return myVar;}
    set { myVar = value;}
}

After the property is generated, the data type is highlighted and can be immediately overtyped. Once the data type is correct, the tab key is pressed for a third time to select the name of the backing store variable, allowing this to be replaced with a more appropriate name. A final press of the tab key selects the property's name, which again is overtyped with the desired text.

Read-Only Property Snippet

Using the "prop" snippet generates a standard read/write property. If you require a read-only property instead, type "propg" and press the tab key twice. This slightly different snippet, shown below, is populated in the same manner as above.

private int myVar;
public int MyProperty
{
    get { return myVar;}
}

Visual Studio 2008 Property Snippet

Visual Studio 2008 permits development of software targeted at several versions of the .NET framework. However, as this version of the IDE is primarily aimed at developers of .NET framework version 3.0 or 3.5 software, the "prop" snippet generates an automatically implemented property, as shown below. The tab key can be used to switch between selecting the data type and the name of the property.

public int MyProperty { get; set; }

Visual Studio 2008 Read-Only Property Snippet

A read-only version of the automatic property snippet is also available in Visual Studio 2008. This snippet creates a property with a public get accessor and a private set accessor, using the "propg" alias.

public int MyProperty { get; private set; }
20 May 2008