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 Property Initial Values

Without defining a constructor, set the starting value of the StartAt property to 10????

The previous video shows us how to set the startat property of health. This question asks us to set the value for I assume, a countdown. So what am I supposed to do here?

CountDown.cs
namespace Treehouse.CodeChallenges
{
    class CountDown
    {
        public int StartAt { get; private set; }
    }
}

1 Answer

Kjetil Lorentzen
Kjetil Lorentzen
13,360 Points

You can initialize StartAt by setting it equal to 10: public int StartAt {get; set private;} = 10

This will set the value to 10 at initialization, but it may be changed later by the private set.

I believe I understand. That is actually really simple :p. but why am I receiving a compiling error when I try to initialize it?

Never mind, I figured out the problem. The private was unnecessary and a semicolon follows the 10. It always seems to be the little things that throw me off in coding. Thank you, Kjetil for your assistance! I really need to think about these better and be more observant. The correct code should be:

namespace Treehouse.CodeChallenges
{
    class CountDown
    {
        public int StartAt { get; set; }=10;
    }
}