You are here: Platform Specific Issues > Automatic Properties

Automatic Properties

Automatic properties simply allow a user to skip the step of introducing private field variables when the property simply sets and gets the field value:

class Person
{
    public string Name { get; set; }
}
AutoProperties.cs: Auto property
Public Class Person
    Public Property Name() As String
End Class
AutoProperties.vb: Auto property

Behind the scenes the compiler creates a field and the regular property code. The field-name depends on the compiler. For C# the field-name of auto-property is <PropertyName>k__BackingField. For example the property FirstName will be stored in the field <FirstName>k__BackingField. For VB.Net the field-name of auto-property is _PropertyName. For example the property FirstName will be stored in the field _FirstName.

public class Person
{
    // the name is actually <Name>k__BackingField 
    // but you cannot express that in C# code
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}
AutoProperties.cs: Auto property behind the scenes
Public Class Person
    Private _Name As String

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
End Class
AutoProperties.vb: Auto property behind the scenes

Issues With Auto Properties

Note that db4o always uses the field name for its configuration. This also applies to auto-properties. You need to use the field-name to specify index etc.

For C# the name of the backing field is not specified and a compiler implementation detail. Therefore it's possible that it actually changes in the future.

For example to index the auto property Name:

IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.ObjectClass(typeof(Person)).ObjectField("<Name>k__BackingField").Indexed(true);
AutoProperties.cs: Configure auto properties
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.Common.ObjectClass(GetType(Person)).ObjectField("_Name").Indexed(True)
AutoProperties.vb: Configure auto properties