4.6. Constructors

When we talked about instantiating objects in the previous chapter, you may have been curious about the interesting syntax involved with the new operator:

Student x = new Student();

In particular, you may have wondered why there were parentheses tacked onto the end of the statement. It turns out that when we instantiate an object via the new operator, we're actually invoking a special type of function member called a constructor. The new operator is used to allocate the memory needed to store the data of the object. The job of the constructor is to initialize the instance variables of the object.

4.6.1.

4.6.1.1. Default Constructors

It turns out that if we don't explicitly declare any constructors for a class, C# automatically ...

Get Beginning C# 2008 Objects: From Concept to Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.