One modest but greatly appreciated feature in VB 10 and Visual Studio 2010 is the inclusion of automatically implemented properties. This feature was first introduced in C# 3/Visual Studio 2008. The idea is that the typical boilerplate property declaration and creation of a field as a backing store can be created through a little extra work done by the compiler.
If you are familiar with the c# version, you will notice some differences in the VB.NET implementation. In the VB.NET version of auto properties, only properties with setters and getters having the same accessibility level. In C#, you can have an auto properties with a public getter and private setter. VB.NET does default to use Public accessibility (, although Private or Protected availability are available. A feature available only in VB.NET auto properties is the convenient ability to assign an initial default value to a property.
The full definition of a property plus its backing store looks like this.
Private _manualProp As String = ""
Property manualProp As String
Get
Return _manualProp
End Get
Set(ByVal value As String)
_manualProp = manualProp
End Set
End Property
The auto property definition looks like this but produces the identical code as with a manual property, if you check the results in reflector. The naming convention used is that the compiler will create a backing store field by prepending an underscore character to the name of the property.
Property autoProp As String = “”
For those folks committing to using a version of the .NET framework prior to 4 there is good news. Since this new feature is created through an enhancement to the compiler in Visual Studio 2010, you can still target .NET 2.0 through 3.5 and take advantage of this new feature.
No comments:
Post a Comment