Every building needs a construction crew that sets things up on day one. In C#, a constructor is a special method that runs automatically when you create an object with new. It initializes the object — no manual setup needed!
A constructor has the same name as the class and no return type — not even void. If you don't write one, C# provides a hidden default constructor that sets all fields to their default values (null, 0, false, etc.).
class Building
{
public string Name;
public int Floors;
// Default constructor
public Building()
{
Name = "Unknown Building";
Floors = 1;
}
// Parameterized constructor
public Building(string name, int floors)
{
Name = name;
Floors = floors;
}
}
This is constructor overloading — multiple constructors with different parameter lists. C# picks the right one based on the arguments you pass:
Building b1 = new Building(); // calls default
Building b2 = new Building("Sky Tower", 50); // calls parameterized
You can chain constructors with the this() keyword — one constructor calls another to avoid duplicating code:
class Building
{
public string Name;
public int Floors;
public string Material;
public Building() : this("Unknown Building", 1, "Concrete")
{
// Delegates to the full constructor
}
public Building(string name, int floors) : this(name, floors, "Concrete")
{
// Delegates to the full constructor with default material
}
public Building(string name, int floors, string material)
{
Name = name;
Floors = floors;
Material = material;
}
}
C# also has static constructors — they run once per class (not per object), automatically before the first use. Great for one-time initialization:
class Building
{
public static int TotalBuildings;
static Building()
{
TotalBuildings = 0; // runs once, before any Building is created
}
public Building()
{
TotalBuildings++; // runs every time a new Building is created
}
}
Default constructor — no parameters, sets defaults
Parameterized constructor — accepts arguments for custom initialization
Constructor overloading — multiple constructors, different signatures
this() — chains to another constructor in the same class
Static constructor — runs once per class, no access modifier, no parameters
Object initializer — new Building { Name = "X" } syntax (sets after constructor runs)
📋 Instructions
**Assemble The Crew!**
1. Create a `Building` class with fields: `Name` (string), `Floors` (int), `Material` (string), and a static field `TotalBuildings` (int).
2. Add a **static constructor** that sets `TotalBuildings` to `0`.
3. Add a **default constructor** that sets Name to `"Unknown Building"`, Floors to `1`, Material to `"Concrete"`, and increments `TotalBuildings`.
4. Add a constructor taking `(string name, int floors)` that chains to the full constructor with Material defaulting to `"Concrete"`.
5. Add a **full constructor** taking `(string name, int floors, string material)` that sets all fields and increments `TotalBuildings`.
6. In `Main`, create three buildings:
- `b1` using the default constructor
- `b2` using `("Sky Tower", 50)`
- `b3` using `("Crystal Palace", 25, "Glass")`
7. Print as shown in expected output.
Use `: this(args)` after a constructor signature to chain. The default constructor chains to the full one with `this("Unknown Building", 1, "Concrete")`. Remember to increment `TotalBuildings` in the full constructor (the one everything chains to).