Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Encapsulation with Properties Accessor Methods

Why does the instructor change the name of the field Location to _location when the field changes to private?

Around 1:46 of the video, the instructor changes the name of the field Location to _location when the field changes to private. Are we then required to change the name of a field when the method access changes to private from public as a standard practice going forward?

Also, why does the name change when the field changes to private?

1 Answer

This is done purely because of naming conflicts with the private field and the constructor taking in a value in the parameter also named location, when instantiating the field. For example a person class that has a name field:

class Person { private readonly string name; public Person(string name) { name = name } } Here we have an issue with both things being called name. To solve this, we can use

this.name = name

where this is referring to the current objects field of name

or we can change

private readonly string name

to

private readonly string _name;