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# ASP.NET MVC Basics Modeling and Presenting Data Updating the Controller

Do controllers usually have constructors, even if they are simple and have no models?

In the video, he added a constructor to the ComicBookController class, but before he added the constructor it still worked. Is it only necessary to add a constructor to a controller when you have private variables?

1 Answer

Steven Parker
Steven Parker
220,378 Points

Classes get default constructors even if you don't write one.

The default constructor takes no parameters, and does nothing except call the also parameterless constructor of the base class. It's equivalent to:

public YourClassName(): base() {}

But you need to add your own constructor if you want something special to happen whenever a new class instance is created.