What is a Constructor?
A constructor runs when an object is created and gives it the starting values it needs; use this to tell a field from a parameter of the same name.
Apprentice · C# · OOP · Updated
After this lesson you can
- Write a constructor that gives an object its starting values
Key takeaways
- A constructor runs once, when
newcreates the object, and gives it the starting values it needs. - A constructor's name is exactly the class's name, and it has no return type, not even
void. - In
this.temperature = temperature;,this.temperatureis the object's field andtemperatureis the parameter. - A field you don't set starts at its type's default, such as
0for anint. - A class can have several constructors with different parameters. A class with none gets an empty parameterless one.
Most objects need starting values to work properly. A refrigerator needs a capacity: if it starts at 0, nothing ever fits.
Refrigerator fridge = new Refrigerator();
Console.WriteLine( quot;Did it fit? {fridge.AddItem(30)}");
public class Refrigerator
{
private int capacity;
private int usedSpace;
public bool AddItem(int itemSize)
{
if (usedSpace + itemSize <= capacity)
{
usedSpace += itemSize;
return true;
}
return false;
}
}
Did it fit? False
A field you don't set starts at its type's default, here 0. Nothing fits in a refrigerator with no room.
Adding a constructor
A constructor is a special method that runs once, when an object is created with new. Its only job is to set the object up. It has no return type (not even void), and its name is exactly the class's name:
Refrigerator fridge = new Refrigerator(37, 1500);
Console.WriteLine( quot;Did it fit? {fridge.AddItem(30)}");
public class Refrigerator
{
private int temperature;
private int capacity;
private int usedSpace;
public Refrigerator(int temperature, int capacity)
{
this.temperature = temperature;
this.capacity = capacity;
this.usedSpace = 0;
}
public bool AddItem(int itemSize)
{
if (usedSpace + itemSize <= capacity)
{
usedSpace += itemSize;
return true;
}
return false;
}
}
Did it fit? True
Now every refrigerator must be created with a temperature and a capacity: new Refrigerator() with no arguments no longer compiles. The constructor makes it impossible to forget.
this
Inside the constructor there are two variables named temperature: the parameter, and the object's field. The word this means the object being created, so:
this.temperatureis the object's field;temperatureon its own is the parameter.
this.temperature = temperature; copies the value passed in into the object. You can use this. anywhere in a class to make clear you mean the object's own member.
More than one constructor
A class can have several constructors, as long as their parameters differ. Callers pick the one that fits:
Refrigerator standard = new Refrigerator();
Refrigerator miniFridge = new Refrigerator(40, 300);
Console.WriteLine(standard.Describe());
Console.WriteLine(miniFridge.Describe());
public class Refrigerator
{
private int temperature;
private int capacity;
public Refrigerator()
{
temperature = 37;
capacity = 1500;
}
public Refrigerator(int temperature, int capacity)
{
this.temperature = temperature;
this.capacity = capacity;
}
public string Describe()
{
return quot;{capacity} units at {temperature} degrees";
}
}
1500 units at 37 degrees
300 units at 40 degrees
A constructor with no parameters is a parameterless (or default) constructor. If a class has no constructor at all, C# gives it an empty parameterless one, which is why new Refrigerator() worked at the start of this lesson.
Check yourself
2 questions to try on the lesson page.
So what
A constructor guarantees that every object starts life ready to work: the values it needs are given at the moment it's made, and the compiler won't let anyone skip them.