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#

Viktor Zhyvotun
Viktor Zhyvotun
2,153 Points

Why are we creating integers with same names in C# course?

Sorry for what might be a dumb question but i just don't fully understand it... So in C# course we are constantly creating 2 different integers. First - we create something like..... SideLength, and then in the constructor we create sideLength (camel-cased) and equalize them. Whats the point of that? Why do we need 2 if they're equal? In which case we use which one?

Something like that :

class Map
  {
    public readonly int Width;
    public readonly int Height;

    public Map(int width, int height)
    {
      Width = width;
      Height = height;
    }

3 Answers

Benjamin Larson
Benjamin Larson
34,055 Points

Not a dumb question at all, it definitely resembles unnecessary, duplicate code, which you know is appropriately discouraged. It can seem especially verbose and inefficient when you start adding getter and setter methods for all of these values.

The first thing to make clear is that the declarations inside of the constructor definition are not the same as variables declared for the class (different languages refer to them as: member variables, properties, attributes or state depending on convention). Inside the constructor, they are input parameters (as with any function or method call). Think of them more as temporary placeholders whereas the class-level variables are more permanent (at least, within the life of a given object).

Those parameters are a way to pass around data. You could hard card values for Width and Height, but that would limit you from retrieving the data from user input or from another web service.

If you only declared Width and Height at the class level, you would have no means of using the constructor to instantiate a new object with arguments for a customized width and height.

If you only declared width and height in the constructor and not in the class, those values would vanish as soon as the constructor method was finished and they wouldn't be saved to the object for use anywhere else.

I hope that helps a little. If you have more questions, feel free to ask.

Viktor Zhyvotun
Viktor Zhyvotun
2,153 Points

Thanks for the answer. Sooo, could I then say that it is a set rule - constructor must always contain parameters that are equal to specific attributes of the class (apart from maybe very specific and rare reasons not to) ?

Benjamin Larson
Benjamin Larson
34,055 Points

Not quite. There's not exactly a typical pattern that can be followed for creating constructors. You'll often find that a class has multiple constructors for this very reason---the usage just can't be predicted so multiple constructors are offered to increase the flexibility of the class. This is called constructor overloading.

Multiple constructors are differentiated by their signature. A method signature is the name of the method, along with the number and type of parameters it accepts. Each constructor needs to have a unique signature, meaning that it either accepts a different number of parameters from every other constructor, or if multiple constructors have the same number of parameters, at least 1 of the parameters must be of a different data type.

You will see this many times when you start reading documentation for most programming languages. You'll find many pre-built methods that accept different numbers of parameters for different circumstances and the reasons will start to be very clear the more you work with them.