| Start |
|---|
| Auto-implemented properties Auto-implemented properties (opens new window) were introduced in C# 3. When an auto-implemented property is written in your code, the compiler creates a private anonymous field that can only be accessed through the property's accessors. The above auto-implemented property statement is equivalent to writing this lengthy code: Auto-implemented properties cannot have any logic in their accessors, for example: An auto-implemented property can however have different access modifiers for its accessors: C# 6 allows auto-implemented properties to have no setter at all (making it immutable, since its value can be set only inside the constructor or hard coded): For more information on initializing auto-implemented properties, read the Auto-property initializers (opens new window) documentation. |
| Public Get Getters are used to expose values from classes. |
| Public Set Setters are used to assign values to properties. |
Accessing Properties |
| Default Values for Properties Setting a default value can be done by using Initializers (C#6) If it is read only you can return values like this: |
Various Properties in Context |
Read-only properties DeclarationA common misunderstanding, especially beginners, have is read-only property is the one marked with A property is read-only when it only has a getter. Using read-only properties to create immutable classesRemarksProperties combine the class data storage of fields with the accessibility of methods. Sometimes it may be hard to decide whether to use a property, a property referencing a field, or a method referencing a field. As a rule of thumb: With regards to Methods vs Properties, where you can both retrieve ( |
Read Article
https://develop.horje.com/learn/1434/reference