Auto-implemented properties are a time saving feature that is new in Visual Basic 2010 - a short-cut for creating class properties.
Auto-implemented properties enable you to declare a class property in a way that leaves it up to Visual Studio to create a private backing field and the Get and Set parts of the property for you.
For example, when you type the following into the Visual Basic 2010 code editor:
Property CustomerId() As Integer
you get a property that is equivalent to declaring a property like this:
Private _CustomerId As Integer
Property CustomerId() As Integer
Get
Return _CustomerId
End Get
Set(ByVal value As Integer)
_CustomerId = value
End Set
End Property
but all you see is:
Property CustomerId() As Integer
You can include a default value:
Property CustomerName() As String = "New"
The backing field name is the auto-implemented property name preceded by an underscore (_). For example, if you declare an auto-implemented property named ID, the backing field is named _ID. Be careful - if you include a member of your class that is also named _ID, you produce a naming conflict and Visual Basic reports a compiler error.
Auto Implement Property Rules
The access modifier for the backing field is always Private, even when the property itself has a different access level, such as Public.
If the property is marked as Shared, the backing field also is shared.
Attributes specified for the property do not apply to the backing field.
The backing field can be accessed from code within the class and from debugging tools such as the Watch window. However, the backing field does not show in an IntelliSense word completion list.
You cannot initialize an auto-implemented property that is a member of an Interface, or one that is marked MustOverride.
When you declare an auto-implemented property as a member of a Structure, you can only initialize the auto-implemented property if it is marked as Shared.
When you declare an auto-implemented property as an array, you cannot specify explicit array bounds. However, you can supply a value by using an array initializer, as shown in the following examples.
Property Grades As Integer() = {90, 73} Property Temperatures As Integer() = New Integer() {68, 54, 71}
You have to use expanded property-definition syntax if you want to do any one of the following:
Add code to the Get or Set procedure of a property, such as code to validate incoming values in the Set procedure. For example, you might want to verify that a string that represents a telephone number contains the required number of numerals before setting the property value.
Specify different accessibility for the Get and Set procedure. For example, you might want to make the Set procedure Private and the Get procedure Public.
Create properties that are WriteOnly or ReadOnly.
Use parameterized properties (including Default properties). You must declare an expanded property in order to specify a parameter for the property, or to specify additional parameters for the Set procedure.
Place an attribute on the backing field.
Provide XML comments for the backing field.
How to expand an Auto-Implemented Property
The Visual Basic Code Editor can automatically generate the the visual Get and Set procedures and End Property statement for the property.
Put the cursor on a blank line following the Property statement, type a G (for Get) or an S (for Set) and press ENTER. The Visual Basic Code Editor automatically generates the Get or Set procedure for read-only and write-only properties when you press ENTER at the end of a Property statement.