It’s not unusual to give a class the capability of exposing data but not of modifying such data. Continuing the example of the Person
class, imagine you want to expose the FirstName
and LastName
properties plus a FullName
property that returns the full name of the person. This property should be marked as read-only because only the FirstName
and LastName
properties should be editable, and FullName
is the result of the concatenation of these two properties. You can therefore define a read-only property as follows:
The ReadOnly
keyword marks properties as read-only. As you can easily understand, auto-implemented properties cannot work here because you need a space to write code for returning a value.
18.221.185.155