When to make a property | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

4th Apr 2019, 11:39 AM
Charles Steward
Charles Steward - avatar
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
4th Apr 2019, 11:56 AM
Anna
Anna - avatar
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
4th Apr 2019, 8:50 PM
sneeze
sneeze - avatar