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 Instantiation

Tower tower = new Tower (); why is this convention used to create a new class?

I was told that writing Tower tower = new Tower () is less confusing than maybe elaborating more on what each of these same words mean and do. I'm not seeing this as less confusing...in fact, very difficult to follow:

Tower <---- the class name, basically telling the computer what type of variable we're creating here (this isn't an int or a string for example)

tower <---- the variable name

new Tower () <---- a new instance of the Tower class...aka the constructor, which acts like a factory to create variable instances of the Tower Class

If anything, why wouldn't you make it like this:

Tower_Class varTower = new Tower_Class ();

1 Answer

Steven Parker
Steven Parker
229,745 Points

You could do that if you want.

Assuming the class is defined appropriately, you certainly could write:

Tower_Class varTower = new Tower_Class ();

But once you get used to the syntax, you'll recognize classes and variables from how they are used (you seemed to be doing a good job of that already above). At that point, attaching "var" or "_Class" to the name will seem unnecessarily verbose.