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

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
231,269 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
231,269 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
231,269 Points

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