Auto-implemented properties make property-declaration more concise when no additional logic is required in the property.
When you declare a property the compiler creates a private, anonymous backing field that can only be accessed through the property’s get and set accessors.
The syntax for automatically implemented properties
When you declare a property the compiler creates a private, anonymous backing field that can only be accessed through the property’s get and set accessors.
The syntax for automatically implemented properties
1
2
3
| //Auto-Property enhancements public string FirstName { get ; set ; } public string LastName { get ; set ; } |
C# 6.0 improves the auto-properties capabilities.
Auto-Property Initializers
Firstly, you can initialize auto-implemented properties similarly to fields. It allows you to declare the initial value for an auto-property as part of the property declaration.
1
2
| // Auto Property Initialization public string FirstNameInitialization { get ; set ; } = "Name" ; |
Read-only auto-properties
Read-only auto-properties provide a more concise syntax to create immutable types.
1
2
3
| // Read Only Auto Property public string ReadOnlyFirstName { get ; private set ; } public string ReadOnlyLastName { get ; private set ; } |
Read-only auto-properties enable true read-only behavior. You declare the auto-property with only a get accessor:
1
2
3
| // True Read-only behavior public string TrueReadOnlyFirstName { get ; } public string TrueReadOnlyLastName { get ; } |
Please refer code sample on GitHub
No comments:
Post a Comment