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 Computed Properties

What are Computed Properties?

What are computed properties and what are they used for?I am just asking to understand what they are better so any answers would be helpful!

2 Answers

Steven Parker
Steven Parker
231,198 Points

Computed properties are data items that you want to make available that differ from what is actually stored, but can be created using them. For example, an object may store a property for first and last name, but may have a computed property that returns the full name. Or it may store only base price and a tax rate, but have a computed property that returns the after-tax price.

Thank you ,Steven :]

Michael Santos
Michael Santos
704 Points

I understand your first/last-name example well. Just to be clear, in our code example, by removing the 'set' keyword from the Location property definition, and making the 'get' custom implemented, I assume we now say the property is no longer an auto-implemented property? In doing so, does this mean that there is no longer an implicit Location variable created "under the hood" for us? I completely understand the rationale for doing this from the perspective of not wanting Invader class users to have to know impl. details like havign to update Location each time _pathStep is incremented, I'm just a little hazy on why this is called a "computed property", in comparison to your first/last-name example which seems more obvious.

Also, do all computed properties by definition not have a setter, or are there exceptions?