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 Properties

2:53 When he calls the variable _location

2:53 When he calls the variable _location, he doesn't use the underscore. This is not necessary for the program to know where he's trying to call it from?

2 Answers

Steven Parker
Steven Parker
231,198 Points

The "location" (no underscore) property provides public access to the private field "_location" (with underscore) field inside the class. The property uses the get and set methods to provide access to the field.

Daniel Crittenden
Daniel Crittenden
9,308 Points

If you tried to use _location, it would be out of scope -- the computer could not access the variable directly from the Main() method.

_location is a private field that is only accessible within the Invader class, specifically by the getter and setter methods. So, in order to access to access _location from Game.cs, you'd actually type invader.Location (or invader.GetLocation() if the applicable getter method was setup).

The location variable the presenter uses on Game.cs was a different one. It is a MapLocation object that was instantiated on line 27 of Game.cs.