0
When to make a property
I was just wondering, if I were to be using object oriented programming, when is the best time to make a property? What is it the most useful for?
2 Answers
+ 4
You're talking about python? Properties are read-only, so you can use them to make sure that instance variables aren't changed without permission, especially in combination with a setter method
class Person():
def __init__(self, age):
self._age = age
@property
def age(self):
return self._age
p = Person(24)
print(p.age) # 24
p.age = 25 # error: can't set attribute
0
Talking about c#
Properties are used for data, you need outside the class.
Please stop using fields, use properties every time.
With a property you can check and validate the input with "set"
With "get" you can control the ouput.
A property enables you to do encapsulation. Protect the data inside the class.
https://www.dotnetperls.com/property