In Example 7-1, notice that the statement that creates the Box
object looks as though it is invoking a Box( )
method, because of the parentheses:
Box boxObject = new Box( );
In fact, a member method is invoked whenever you instantiate an object. This method is called a constructor. Each time you define a class, you are free to define your own constructor, but if you don’t, the compiler will provide one for you invisibly and automatically.
The job of a constructor is to create an instance of the object specified by a class and to put it into a valid state. Before the constructor runs, the object is just a blob of memory; after the constructor completes, the memory holds a valid instance of the class.
The Box
class of Example 7-1 does not define a constructor, so the compiler implicitly provides one. The constructor provided by the compiler creates the object but takes no other action.
Any constructor that takes no arguments is called a default constructor. The constructor provided by the compiler takes no arguments, and hence is a default constructor. This terminology has caused a great deal of confusion. You can create your own default constructor, and if you do not create a constructor at all, the compiler will create a default constructor for you, by default.
If you do not explicitly initialize your member variables, they are initialized to innocuous values (integers to 0, strings to an empty string, and so on). Table 7-2 lists the default values assigned to some of the common types.
Table 7-2. Primitive types and their default values
Type | Default value |
---|---|
Numeric ( | 0 |
| False |
| The null character ( |
| 0 |
Reference | null |
Typically, you’ll want to define your own constructor and provide it with arguments so that the constructor can set the initial state for your object. In Example 7-3, you’ll pass in some initial values for Box
so that the object is created with meaningful data.
You declare a constructor like you do any other member method, except:
If there are arguments to be passed, you define an argument list just as you would for any other method. Example 7-3 declares a constructor for the Box
class that accepts three arguments, one each for the length, width, and height for the new Box
object you are creating.
Example 7-3. To create a constructor, you create a method with the same name as the class that initializes the internal variables
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Example_7_3_ _ _ _Constructor { public class Box { // private variables private int length; private int width; private int height; // public methods public void DisplayBox( ) { Console.WriteLine("Length: {0}, Width: {1}, Height: {2}", length, width, height); } // constructor public Box(int theLength, int theWidth, int theHeight) { length = theLength; width = theWidth; height = theHeight; } } public class Tester { static void Main( ) { Box boxObject = new Box(4, 8, 3); boxObject.DisplayBox( ); } } }
The output looks like this:
Length: 4, Width: 8, Height: 3
In this example, the constructor takes a series of integer values and initializes all the member variables based on these parameters.
When the constructor finishes, the Box
object exists and the values have been initialized. When DisplayBox( )
is called in Main( )
, the values are displayed.
Try commenting out one of the assignments and running the program again. You’ll find that each member variable that isn’t assigned to by you is initialized by the compiler to zero. Integer member variables are set to zero if you don’t otherwise assign them. Remember that value types (such as integers) must be initialized; if you don’t tell the constructor what to do, it sets innocuous values.