What is an auto-implemented field

2019-09-05 00:58发布

问题:

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

回答1:

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.



回答2:

Well, to answer: yes, internally this is transformed to a full property section. You can even access _Buses in your code.

But you should optimally set a default as well (not mandatory though):

Public Property Buses As Integer = 0

The drawback using this short syntax is that you cannot perform validation on the values during its Set() (you can of course do this when you need to use the value, but that kind of or in part defeats the purpose of using properties). If you are building a user-control you would probably do things such as Invalidate and so forth on the Set(), so although easier on the eye (and fingers), it is very often better to use the full section IMHO.