Classes & Objects — Blueprints Come Alive
10 XPEasy
Ctrl+Enter RunCtrl+S Save
Architect, every great city begins with blueprints. In C#, a class is the blueprint — it defines what a building can be. An object is the actual building constructed from that blueprint. One blueprint, unlimited buildings!
Think of it this way: you don't live inside a blueprint. You live inside a building that was created from a blueprint. The blueprint (class) says "this building has a name, floors, and material." The actual building (object) says "I am Tower Alpha, 30 floors, made of Steel."
// The BLUEPRINT (class)
class Building
{
public string Name; // field: what's it called?
public int Floors; // field: how many floors?
public string Material; // field: what's it made of?
}
The class keyword declares a new type. Inside, we define fields — variables that belong to each object. The public keyword means anyone can access these fields (more on access control later!).
To create an object (build from the blueprint), we use the new keyword:
// Creating an OBJECT (actual building)
Building tower = new Building();
tower.Name = "Tower Alpha";
tower.Floors = 30;
tower.Material = "Steel";
Console.WriteLine($"Building: {tower.Name}");
Console.WriteLine($"Floors: {tower.Floors}");
Console.WriteLine($"Material: {tower.Material}");
The dot operator (.) is how you reach inside an object to access its members. tower.Name means "the Name field of the tower object."
class — declares a blueprint (reference type)
new — creates an object (instance) from the class
Fields — variables declared inside a class
Dot notation (.) — accesses members of an object
Each object has its own copy of the fields — changing one doesn't affect another
You can create multiple objects from the same class, each with different data — just like constructing two different buildings from the same blueprint style:
Building a = new Building();
a.Name = "Tower Alpha";
a.Floors = 30;
Building b = new Building();
b.Name = "Garden Plaza";
b.Floors = 5;
// a.Floors is still 30 — each object is independent!
📋 Instructions
**Your City Awaits!**
1. Create a `Building` class with three public fields: `Name` (string), `Floors` (int), and `Material` (string).
2. In `Main`, create the first building object:
- Name: `"Tower Alpha"`, Floors: `30`, Material: `"Steel"`
3. Print its details in the format shown.
4. Create a second building object:
- Name: `"Garden Plaza"`, Floors: `5`, Material: `"Wood"`
5. Print its details in the same format.
Output must match exactly!
Define a class with `class Building { ... }` and add public fields inside. Use `new Building()` to create objects, then assign values with dot notation like `myBuilding.Name = "...";`