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

Ilya Sikharulidze
PLUS
Ilya Sikharulidze
Courses Plus Student 1,073 Points

I'm stuck!

I think I didn't understand really well what is a computed property and because of that I have no idea what to do here, can someone please help me!

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;
        }
    }
}

2 Answers

Steven Parker
Steven Parker
218,654 Points

A computed property is just one that returns a value from a calculation using other fields instead of a value that was previously set. Computed properties often have only a "get" method and no "set".

Ilya Sikharulidze
Ilya Sikharulidze
Courses Plus Student 1,073 Points

Thank you, but I still don't really understand what I have to do here

Steven Parker
Steven Parker
218,654 Points

Well, you have "SideLength" as an example of an auto property. For a computed property, the syntax will be similar but the "get" will have a body with code in it and the "set" can be omitted.

For the Area, the body code of the "get" can use "SideLength" to calculate the area and return it.

Give it a shot, and if you still have trouble, show the code you have for it so far.

Ilya Sikharulidze
PLUS
Ilya Sikharulidze
Courses Plus Student 1,073 Points

I have been able to complete the challenge! Thanks for the help!

Steven Parker
Steven Parker
218,654 Points

Ilya Sikharulidze — Good job! :+1: You can mark a question solved by choosing a "best answer".
And happy coding!