
Hi Michael,
The pattern B you mentioned is called auto-implemented properties
introduced in C#3.0, which make property-declaration more concise in case
where no additional logic is required in the property accessors.
When you declare an auto-implemented property, the compiler creates a
private, anonymous backing field that is not accessible except through the
property's get and set accessors. Other than this, there isn't any
difference between an auto-implemented property and a normal property(by
the normal property, I mean the pattern A you mentioned).
If we set the value of a property in the constructor of the class or in the
declaration statement, the property is only initialized. Note, the default
value is NOT the initial value. If the value of the property equals to the
default value, the property's value is displayed in a regular font in the
Properties window and will not be serialized in the InitializeComponent
method of the Form; otherwise, shown in bold and will be serialized in the
InitializeComponent method.
To set a default value for a property, we can use the DefaultValueAttribute
on the property. And the default value specified by the
DefaultValueAttribute should equal to the initial value of the property.
A variable of type bool has an initial value of false. In your practice,
you don't set the MyBool property's value explicitly, so its initial value
is false. Then you specify the default value of the MyBool property is true
using the DefaultValueAttribute. When you add the UserControl on a form,
the value of the MyBool property shown in the Properties window is false
and in bold, because it doesn't equals to the specified default value.
Hope I make some clarifications.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.