Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

April 11, 2010

Auto-Implemented Properties

Filed under: C#,VB.NET @ 8:21 pm

One of the new features in VB 10.0 is auto-implemented properties. Auto-implemented properties are a shorter syntax for your property statements that automatically implement a backing field so you don’t have to manually define one.

(C# has had auto-implemented properties since C# 3.0. The examples here show both C# and VB for completeness.)

In this example, the Customer class has four properties as shown below.

In VB:

Public Class Customer
   
Public Property CustomerId As Integer
    Public Property FirstName() As String
    Public Property LastName() As String
    Public Property EmailAddress() As String
End Class

In C#:

public class Customer
{
    public int CustomerId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string EmailAddress { get; set; }
}

Without auto-implemented properties, the code is MUCH longer. The code shown above is functionally equivalent to the following:

In VB:

Public Class Customer
   
Private _CustomerId As Integer 
    Public Property CustomerId() As Integer
        Get
            Return _CustomerId
        End Get
        Set(ByVal value As Integer)
            _CustomerId = value
        End Set
    End Property

    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

    Private _LastName As String   
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Private _EmailAddress As String
    Public Property EmailAddress () As String
        Get
            Return _EmailAddress
        End Get
        Set(ByVal value As String)
            _EmailAddress = value
        End Set
    End Property
End Class

In C#:

public class Customer

    private int _CustomerId;
    public int CustomerId
    {
        get { return _CustomerId; }
        set { _CustomerId = value; }
    }

    private string _FirstName;
    public string FirstName
    {
        get { return _FirstName; }
        set { _FirstName = value; }
    }

    private string _LastName;
    public string LastName
    {
        get { return _LastName; }
        set { _LastName = value; }
    }

    private string _EmailAddress;
    public string EmailAddress
    {
        get { return _EmailAddress; }
        set { _EmailAddress = value; }
    }
}

Use auto-implemented properties whenever you need to define a property that does not require specialized code in the getter or setter.

Enjoy!

NOTE: For more information on the features and limitations of auto-implemented properties, see Auto-Implemented Properties Part II.

7 Comments

  1.   Darren — April 12, 2010 @ 6:51 pm    Reply

    Just a note: VB has an extra feature in this regard with intializers – the ability to set default property values

    e.g. Public Property CustomerId As Integer = -1

  2.   Rostov — April 16, 2010 @ 8:50 am    Reply

    These are a fantastic way to not only reduce dev time but also keep a cleaner codefile.

    Also, in case anyone might wonder, you can use the shorthand style and keep pieces of it private:

    public class Customer
    {
    public int CustomerId { get; set; }
    public string LastName { get; private set; }
    public string FirstName { get; set; }
    public string EmailAddress { private get; set; }
    }

  3.   Neal — April 28, 2010 @ 11:34 am    Reply

    People need to also know that Auto-Implemented properties cannot be made Com visible like standard properties.

  4.   Richard — May 28, 2010 @ 10:59 am    Reply

    @Rostov:
    C# allows auto-properties to have a different access modifier applied to the get or set accessor; VB does not.

  5.   Doug Kimzey — July 29, 2010 @ 12:10 pm    Reply

    I like the idea of shorter syntax. I don’t like not knowing what the name of the associate private member functions are.

    What about cases where you want to use the private variables in a class function?

    Is there a way to show the additions it is making to your class?

    Is there a way to disable this?

  6.   DeborahK — July 31, 2010 @ 6:06 pm    Reply

    Hi Doug –

    Which language are you using?

    There is no way I know of to disable the ability to use auto-implemented properties in either language.

  7.   .Net Training — November 9, 2012 @ 1:53 am    Reply

    Auto Implemented Properties does not support read-only or write-only properties.

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2023 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs