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

Code Challenge

I dont understand this code challenge.

Square.cs
namespace Treehouse.CodeChallenges
{
    class Square : Polygon
    {
        public double SideLength { get; private set; }

        public Square(double sideLength) : base(4)
        {
            SideLength = sideLength;
        }
    }
}
Polygon.cs
namespace Treehouse.CodeChallenges
{
    public class Polygon
    {
        public int NumSides { get; private set; }

        public Polygon(int numSides)
        {
            NumSides = numSides;
        }
    }
}

1 Answer

Kjetil Lorentzen
Kjetil Lorentzen
13,360 Points

The code challenge is about computed properties (auto properties). The goal is to create a property, that automatically return a computed value when called.

In this specific case you want to create a property named Area that returns the computed area of the square when called. This can be used by specifying a calculation in the getter of the Area property:

public double Area
{
get {
 return SideLength*SideLength;
}
}

When calling Square.Area the getter will automatically calculate the area from SideLength, and the area will always use the latest value of SideLength.