86Microsoft Visual Studio 2010: A Beginner’s Guide

Listing 3-10 Auto-implemented properties

C#:

public string FirstName { get; set; }

VB:

Public Property FirstName As String

Figure 3-4 The C# property snippet template

The automatic property, FirstName, is logically equivalent to the expanded FirstName with accessors and backing field. Behind the scenes, the compiler produces the expanded version where the backing field is guaranteed to have a unique name to avoid conflicts.

Do not overlook that when you use automatic properties, you cannot add your own code that runs inside the get or set accessors.

The Property Snippet

To create a property snippet, type pro and press TAB, TAB; and you’ll see the property snippet template shown in Figure 3-4 for C# or Figure 3-5 for VB.

A C# property snippet template creates an automatic property by default, but the VB snippet template is a normal property with full get and set accessors.

Figure 3-5 The VB property snippet template

Page 109
Image 109
Microsoft 9GD00001 manual Property Snippet, Listing 3-10 Auto-implemented properties