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
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
_Busesaslong 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
GetandSetparts of the property. The general definition of an auto-implemented property is as follows:or
In both cases, the compiler generates all the backing fields and initializers for you automatically.
Consider the following class with two auto-implemented properties (
NameandAge) and one regular property (Address).The compiler automatically generates backing fields as well as
GetandSetmethods for theNameandAgeproperties.The fields generated have the same name as the property with a preceding underscore. Therefore the
Nameproperty’s backing field is_Nameand theAgeproperty’s backing field is_Age.The auto-generated fields also have the
DebuggerBrowsable(DebuggerBrowsableState.Never)andCompilerGeneratedattributes attached to them.The
DebuggerBrowsableattributes 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 theToStringmethod where I use the_Namefield directly.The
CompilerGeneratedattribute indicates that the field was created by the compiler.The
Ageproperty (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:
As you can see, the fields
_Nameand_Ageare generated for you automatically so you can use them in your code without any problems.