Pro EP 20 : Multiple types of properties in C#

Muhammad Waseem
Become .NET Pro !
2 min readMar 5, 2023

Properties are first class citizens in C#. Those enable developers to write code that accurately expresses their design intent.

Using C# properties we can assign and retrieve values from private fields, we use two keywords for properties set and get. Set accessor is used to assign new value and get accessor is used to retrieve the value of property.

Properties are used to achieve the abstraction; properties behave like fields when they are accessed but properties are implemented via accessors as I described above. Let’s see different types of properties.

  1. Read only Properties
    These properties are used as a getter , we can only used them for retrieving information if we try to use them for setting it will give error.

2. Write only Properties
These properties serve the cause of setting the information, but we cannot use them for reading data.

3. Auto implemented Properties
Auto implemented properties can serve both purpose reading and writing.

4. Init only Properties
Init only properties are used to set data only during object initialization. This could be helpful while creating read only properties.

If you want to help the author in growing

  1. Subscribe my YouTube Channel where I would be sharing videos on .NET
  2. Subscribe my Weekly .NET Newsletter of C#/.NET
  3. Download my eBook at Gum road that contains 30+ .NET Tips.
  4. Become Patron to get access to 80+ Compiled Questions and Answers at one place

--

--