Auto-implemented Properties in VB and C#

| by Jonathan Allen Follow 462 Followers on Jun 15, 2009. Estimated reading time: 1 minute |

A note to our readers: You asked so we have developed a set of features that allow you to reduce the noise: you can get email and web notifications for topics you are interested in. Learn more about our new features.

C# added auto-implemented properties in version 3, but Visual Basic was unable to match them at that time. With the impending release of .NET 4.0, VB has caught up in this area, but with a distinctive twist. In C#, auto-implemented properties are written as such:

public string FirstName {get; set;}

Visual Basic requires the keyword Property, which means it doesn’t need the get/set pair to distinguish it from a field. Thus auto-implemented properties in VB are only one token away from a field.

Public Property FirstName As String

For both VB and C# the property is automatically backed a private field. In C# this member has an automatically generated name such as “<FirstName>k__BackingField”. Needless to say, this field isn’t accessible using normal C# syntax. Visual Basic is far less creative, it merely takes the property name and prepends an underscore. So if you property is named “FirstName”, there will be a matching private field called “_FirstName”.

Having direct access to the backing field should make more things possible. Logic says one should be able to do things like mark the property ReadOnly and set its value in the constructor. Alas, that is not an option.

C#, despite its seeming limitation, is actually more flexible. You can mark the setter as private or protected, effectively getting the same effect as having access to the backing field.

public string FirstName {get; private set;}

A limitation for both languages is that they still don’t have any facilities for immutable objects. If you want objects where the properties and fields are all read-only and set in constructors, you still have to write the same code you would write in .NET 1.0.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Beware BinaryFormatter by Marc Gravell

Because BinaryFormatter is (by default) a field-based serializer, it will get very upset switching from auto properties to regular fields (since the name matters). For a fuller discussion, see here

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you