Auto Implemented Properties in VB 10


One of the focus areas and strategy for both VB10 and C# 4 (released with .NET Framework 4 and VS 2010) of “co-evolution”, which means that when adding a major feature to one language it would be added in all the languages. Till .NET Framework 3.5 we’ve seen certain important features existing in both VB and C# that do not exist in their counterparts.

C# 3.0 introduced a feature called Auto-Implemented Property – in case the developer does not want to use code in get and set parts of properties, they can use Auto Implemented Property, where compiler automatically generates backup variables –

Normal Property (C#):

public class Employee

    {

        private string _firstName;

        public string FirstName

        {

            get

            {

                return _firstName;

            }

            set

            {

                _firstName = value;

            }

        }

    }

 

Auto Implemented Property (C#):

 

public class Employee

    {

        public string FirstName { get; set; }

    }

 

Now VB 10 also have introduced auto implemented properties similar to C# 3.0 –

Normal Property (VB):

Public Class Employee

 

    Private _firstName As String

    Public Property FirstName() As String

        Get

            Return _firstName

        End Get

        Set(ByVal value As String)

            _firstName = value

        End Set

    End Property

 

End Class

 

New Auto Implemented Property (VB):

 

Public Class Employee

 

    Property FirstName() As String

 

End Class

 

Omitting the bracket after property name is also allowed –

 

Public Class Employee

 

    Property FirstName As String

 

End Class

 

In case of C#, the compiler automatically generates the private backup field/variable that holds the value of the property as shown below.

 

Similarly, in case of VB also the compiler generates the backup field/variable that holds the value of the property – the compiler names it as _ appended by property name (in this case _FirstName). It will also fill in the property’s getter and setter.

 

VB 10 goes a step forward and allows to initialize or set the default value of the property while declaring it –

Property FirstName As String = "Aniruddha"

The default values is set in the constructor by the complier, which can be seen by inspecting the ctor in ILDASM (highlighted) –

 

I know that C# programmers would be jealous by the default value thing.

Auto-implemented properties cannot have parameters, nor can they be declared ReadOnly or WriteOnly.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s