C# ClassesLesson 29Free preview
C# Properties
This lesson is part of C# · All levels
Summary
The property provides controlled access to the private field through the get block for reading and set for writing. C# supports auto-implemented properties that write boilerplate fields behind the screen. The property allows validation to be performed when data is read or written without opening the field directly.
Key points
- Properties consist of accessory get and set, and can be get-only if only legible.
- Auto-property like public int Age {get; set;} makes the private field automatic behind the scenes.
- The init accessor allows the setting of values only when the object is created.
- Validation can be placed on the block set so that the object data is always logical.
- To the user, the property looks like a normal variable even though there's logic behind it.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.