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 Object-Oriented Programming Object Initialization

Constructor method clarification

Other constructor methods can be called into different classes. So how does the class know when it is constructing itself with its own variables are being used and are readonly so they can not be changed after construction. Basically, how can you change the value of the readonly variable in the Map costructor if the Map class doesnt allow its variable to change. Does the Map class somehow know it is being constructed in the Map constructor so it waits for the construction to finish before it doesnt allow you to edit the readonly variables anymore?

Also, what is the purpose of the readonly height and width variables in the class. why not just have the other width and height variable initialized in the constructor parameters? Why have it assigned to two public variables that cant change?

Does the Map constructer in the video not have to be named "Map" is that what you're saying? You can just have multiple different constructors with different names in the map class, but they would all just be map constructers and not constructors for different classes?

2 Answers

Steven Parker
Steven Parker
229,644 Points

You're essentially right, the read-only aspect of the variables doesn't apply inside the constructor. But otherwise they cannot be changed.

And the reason for making height and width read-only for this class is that they indicate the size of the storage that was created in the constructor. Since these sizes don't change once the object is created, it makes sense that the variables that report the sizes shouldn't be able to be changed either.

Thank you for answering. But does that mean that the read only variables in the Map class can also be configured in another constructer other than the Map constructer? Or no?

Steven Parker
Steven Parker
229,644 Points

I don't think I know what you mean by "another constructor". Do you mean one overloaded with a different signature? If so, it would still be a "Map constructor"; and yes, the read-only variables can be assigned within it.

Steven Parker
Steven Parker
229,644 Points

A constructor will always have the name of the class, and a constructor for one class will never be found in a different class. While the name of all constructors must be the same, you can have constructors with different signatures (different types or numbers of arguments). Only one constructor will run when an object is created, the one that matches the arguments given for object creation.

Harold Thompson
Harold Thompson
18,157 Points

Steven Parker thank you for explaining constructors!!!