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 and Arrays Encapsulation

Simon Andersson
Simon Andersson
6,880 Points

Confused on "this"

Hi. When the constructor is created, you say "this refers to the CURRENT object". What do you mean by the current object?

3 Answers

Steven Parker
Steven Parker
229,982 Points

:point_right: The current object is the one being created when the constructor runs.

The constructor code only runs when an instance of the class is being created. Inside the constructor, "this" represents that new instance.

Alexandre C.
Alexandre C.
1,245 Points

I am a little bit confused about encapsulation. I understand that if I use the class, I can't modify the array, because it uses the public MapLocation/path, which also uses the private MapLocation/_path

What I don't understand is what prevents me to use the MapLocation in Main. Could'nt I acces this Main/MapLocation and change the values of the array?

I hope you understand me, I'm pretty newbie into the coding-world.

Thanks, Alex

Maciej Sitko
Maciej Sitko
16,164 Points

Alexandre Crespi : What you are talking (i believe) is creating array as a static property, on the Class, not the Object created.

What for in this particular case? Classes are merely blueprints for creating many unique objects, with unique properties and/or methods.

Static is what you talk about (i presume), and its when you want to have something that belongs to the class, not the object created as a result of instantiation process.

You would achieve it in the following way:

class MapLocation {
    // This would be accessible on the class itself
    public static string[] staticStringArray;

   // (...) rest of the code

// elsewhere
MapLocation.staticStringArray = new []{ "string1", "string2" };

This is not encapsulated now: it is available to everyone, everywhere, on the class.

BTW: Encapsulation is about hiding your sensitive data from global access, and users, that could for instance exploit it, as for example, public static is something accessible to everyone, everywhere.

Alexandre C.
Alexandre C.
1,245 Points

Ok, thank you very much for your help.