Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: