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!

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 Expression Bodied Members

can't solve challenge 1 of 2

Challenge Task 1 of 2

Use the syntactic sugar just learned about to convert the Area property to a single line property. Important: In each task of this code challenge, the code you write should be added to the code from the previous task. Restart Preview Get Help Check Work Square.cs Polygon.cs

1 namespace Treehouse.CodeChallenges 2 { 3 class Square : Polygon 4 { 5 public double SideLength { get; private set; } 6 ā€‹ 7 public double Area 8 { 9 get { return SideLength * SideLength; } 10 } 11

12 public Square(double sideLength) : base(4) 13 { 14 SideLength = sideLength; 15 } 16

17 public void Scale(double factor) 18 { 19 SideLength *= factor; 20 } 21 } 22 } 23 ā€‹

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

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

        public Square(double sideLength) : base(4)
        {
            SideLength = sideLength;
        }

        public void Scale(double factor)
        {
            SideLength *= factor;
        }
    }
}
Polygon.cs
namespace Treehouse.CodeChallenges
{
    class Polygon
    {
        public int NumSides { get; private set; }

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

2 Answers

Hi Shawn! So there are a couple different ways to write a property in C#. One way is to use the syntax:

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

Another is (and this is the style the challenge is asking for) is to write it on one line using the "=>" which is essentially the same as the "get { return }" part in the other property style. That "syntactical sugar" style looks like this:

public double Area => SideLength * SideLength;

Hope that helps! the next challenge you should be able to solve given this information :)

thank's tj

Thanks for this answer-

One quick question I have (I may be missing something super basic) is why we no longer have to use the return keyword here?

Any help is appreciated!

This passed for me and its explained starting at 1:27 into the video: namespace Treehouse.CodeChallenges { class Square : Polygon { public double SideLength { get; private set; }

    public double Area => SideLength * SideLength;


    public Square(double sideLength) : base(4)
    {
        SideLength = sideLength;
    }

    public void Scale(double factor)
    {
        SideLength *= factor;
    }
}

}