Answered Understanding properties

Jules

New member
Joined
Feb 19, 2021
Messages
1
Programming Experience
Beginner
Can anybody help a real newb understand the purpose of propertise? :)

I understand that they are used to access and update private fields. But isn’t that what I am doing with constructors already?
Aren’t I setting the values for fields in another class through the constructor?

Many thanks,
Jules
 
A constructor is for constructing a type. That may or may not include setting one or more fields and/or properties.

Properties can be accessed any time after the object has been created. You can get the current value of a property or you can set it. A TextBox wouldn't be much use if you couldn't access the Text property after the initial creation of the object.

The point of properties is that they behave like fields from the outside but they behave like methods from the inside. From the consumer's perspective, it's just a value going in or coming out, just like a field. From inside the object, the logic to provide or process that value can be as complex as you like. Two of the most common things to do inside a property setter are validation and raising a change event, neither of which can be done with simple fields.
 
Back
Top Bottom