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

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

Path _path

I don't understand why do we have: private readonly Path _path Can someone please explain! And also, why when we create "Invader" constructor we write: Invader(Path path) ?

1 Answer

Steven Parker
Steven Parker
231,248 Points

Let's break that down:

  • private :point_left: this will only be available to code inside the class
  •     readonly :point_left: after the instance is created, it cannot be changed
  •         Path :point_left: it is an item of type "Path"
  •             _path :point_left: and it's name is "_path"

And when you create an instance, you pass the Path to the constructor, which is why it has that parameter. Inside the constructor, the field value is set ("_path = path;"), and it will be permanent for the life of the instance.

Cristina Rosales
Cristina Rosales
10,929 Points

does it read as private readonly Path _path' because we are using _path as the attribute name and the argument name from the constructor?

would private readonly Path _path = _path; technically work (is what's going on under the hood) aswell?