Search code examples
vb.netvisual-studiovisual-studio-express

What is an auto-implemented field


I have a vb Class with the following Property in it:

Public Property Buses As Integer

Is this equivalent to a more detailed property?

Does the compiler actually transform, in the background, this line of code into a more detailed Property structure which includes a field _Buses?

Therefore without actually declaring _Buses aslong as I use the structure Public Property x AS y then one of these fields will be available?


EDIT
Actually not too sure if much more can be added than HERE ON MSDN


Solution

  • Short Answers

    Q: Does the compiler actually transform, in the background, this line of code into a more detailed Property structure which includes a field _Buses?
    A: Yes

    Q: Without actually declaring _Buses aslong as I use the structure Public Property x AS y then one of these fields will be available?
    A: Yes


    Explanation (Long Answer)

    Auto-implemented properties are generally properties where you do not explicitly specify code for the Get and Set parts of the property. The general definition of an auto-implemented property is as follows:

    Public Property Age As Integer
    

    or

    Public Property Age As Integer = 5
    

    In both cases, the compiler generates all the backing fields and initializers for you automatically.

    Consider the following class with two auto-implemented properties (Name and Age) and one regular property (Address).

    Public Class Person
    
        Dim _address As String
    
        Public Sub New()
            _address = "4, Hutchinson Road"
        End Sub    
    
        Public Property Name As String
        Public Property Age As Integer = 3
        Public Property Address As String
            Get
                Return _address
            End Get
            Set(value As String)
                _address = value
            End Set
        End Property
    
        Public Overrides Function ToString() As String
            Return _Name & " Age: " & Me.Age.ToString()
        End Function
    End Class
    

    The compiler automatically generates backing fields as well as Get and Set methods for the Name and Age properties. The fields generated have the same name as the property with a preceding underscore. Therefore the Name property's backing field is _Name and the Age property's backing field is _Age.

    The auto-generated fields also have the DebuggerBrowsable(DebuggerBrowsableState.Never) and CompilerGenerated attributes attached to them.

    The DebuggerBrowsable attributes prevents the field from being displayed in the Auto-Complete list in the code editor. This, however, does not prevent you from accessing the field directly in your code, as you can see in the ToString method where I use the _Name field directly.
    The CompilerGenerated attribute indicates that the field was created by the compiler.

    The Age property (as well as all auto-implemented properties with initializers) is initialized in the class's default constructor.

    The decompiled version of the class above looks like this:

    Public Class Person
        ' Methods
        Public Sub New()
            Me.Age = 3
            Me._address = "4, Hutchinson Road"
        End Sub
    
        Public Overrides Function ToString() As String
            Return String.Join(" ", New String() { Me._Name, Me.Age.ToString })
        End Function
    
    
        ' Properties
        Public Property Address As String
            Get
                Return Me._address
            End Get
            Set(ByVal value As String)
                Me._address = value
            End Set
        End Property
    
        Public Property Age As Integer
            <DebuggerNonUserCode> _
            Get
                Return Me._Age
            End Get
            <DebuggerNonUserCode> _
            Set(ByVal AutoPropertyValue As Integer)
                Me._Age = AutoPropertyValue
            End Set
        End Property
    
        Public Property Name As String
            <DebuggerNonUserCode> _
            Get
                Return Me._Name
            End Get
            <DebuggerNonUserCode> _
            Set(ByVal AutoPropertyValue As String)
                Me._Name = AutoPropertyValue
            End Set
        End Property
    
    
        ' Fields
        Private _address As String
        <CompilerGenerated, DebuggerBrowsable(DebuggerBrowsableState.Never)> _
        Private _Age As Integer
        <DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated> _
        Private _Name As String
    End Class
    

    As you can see, the fields _Name and _Age are generated for you automatically so you can use them in your code without any problems.