Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Encapsulation with Properties Properties

Each time we use a property, must we also set the setter to the property as part of that process of using a property?

In 3:52 of this video, the presenter says that the setter is also set to the location property. Is this a step that must be performed each time we use a certain property, that is, we have go to the setter and set it to that property as well?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Ra Bha! It depends on whether or not that property is private or not. I definitely recommend keeping them private. The reason to do this is to avoid things getting inadvertently changed by accident. Imagine a scenario where you're trying to ask if that property is equal to something, but you forget to type a double equal like == and instead type a single equal =. That would change the value of the property because it's an assignment. It wouldn't produce any error, but suddenly it has a totally new value.

However, if you set the property to be private, then it won't allow you to do things like that without throwing an error. Instead, it forces you to use the setter to set the value. It makes it so that if you want to change it, you have to be really intentional about doing so :smiley:

Hope this helps! :sparkles: