Auto-Implemented Properties

Posted Sun, Apr 11 2010 18:21 by Deborah Kurata

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.

Filed under: , , ,

Comments

# re: Auto-Implemented Properties

Monday, April 12, 2010 6:51 PM by Darren

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

# re: Auto-Implemented Properties

Friday, April 16, 2010 8:50 AM by Rostov

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; }

}

# re: Auto-Implemented Properties

Wednesday, April 28, 2010 11:34 AM by Neal

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

# Collection Initializers

Thursday, April 29, 2010 5:08 PM by Deborah's Developer MindScape

One of the new features in VB 10.0 is collection initializers. Collection initializers allow you to initialize

# Generics: Building a List of Customers

Thursday, April 29, 2010 5:14 PM by Deborah's Developer MindScape

Often times applications require lists of things: lists of customers, lists of experiments, lists of

# Bay.NET Event: .NET 4.0 and VS 2010

Friday, May 14, 2010 7:52 PM by Deborah's Developer MindScape

We are lucky to have two of Redmond's finest down here in Silicon Valley for a full day .NET 4.0

# re: Auto-Implemented Properties

Friday, May 28, 2010 10:59 AM by Richard

@Rostov:

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

# re: Auto-Implemented Properties

Thursday, July 29, 2010 12:10 PM by Doug Kimzey

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?

# re: Auto-Implemented Properties

Saturday, July 31, 2010 6:06 PM by Deborah Kurata

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.

# Auto-Implemented Properties Part II

Friday, August 13, 2010 12:51 AM by Deborah's Developer MindScape

This prior post covered the basics of auto-implemented properties in C# and VB. This current post takes

# Sorting Lists with Null Values

Saturday, October 30, 2010 2:04 AM by Deborah's Developer MindScape

When working with data, you often have values that are null. For example, you may allow entry of a user's

# Building a List of of Items with Child Items

Saturday, October 30, 2010 7:47 PM by Deborah's Developer MindScape

In this prior post , I demonstrated how to build a generic list of items. The example built a list of

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: