Classes and Objects in C#
Think about your favorite online bookstore. It keeps track of thousands of books, each with a title, author, price, and availability. But notice something interesting:
Every book follows the same structure yet each book has its own unique information.
This is exactly how programming models the real world. C# lets you create classes that act like blueprints, and objects that act like real items built from those blueprints.
Today, you’ll learn how to model real things, books, cars, students, anything, using classes, fields, methods, and constructors. And you’ll build your first Book Library Model.
The Project: Book Library Model
You’ll design a real-world concept, a book, and create:
- A Book class
- Fields like title, author, and price
- Methods to display book info
- Constructors to create books easily
The program will store and display multiple books like a mini library system.
Understanding Classes
A class is a blueprint.
Example:
1 2 3 4 5 6class Book { public string title; public string author; public int pages; }
This doesn’t create a book yet, it only describes what a book should have.
Understanding Objects
An object is a real instance created from the class.
1 2 3 4Book b1 = new Book(); b1.title = "The Alchemist"; b1.author = "Paulo Coelho"; b1.pages = 200;
Now you have a real book stored in memory.
Fields (Variables Inside a Class)
Fields store information about an object.
1 2 3public string title; public string author; public double price;
Each object gets its own copy.
Methods (Actions of the Object)
A method describes behavior, what the object can do.
1 2 3 4public void DisplayInfo() { Console.WriteLine($"{title} by {author}"); }
Constructors
A constructor sets initial values when the object is created.
Default Constructor
Executes automatically and sets basic defaults.
1 2 3 4 5 6public Book() { title = "Unknown"; author = "Unknown"; pages = 0; }
Parameterized Constructor
Lets you pass values when creating the object.
1 2 3 4 5 6public Book(string t, string a, int p) { title = t; author = a; pages = p; }
Now creating a book becomes:
1Book b1 = new Book("1984", "George Orwell", 328);
Let’s Build the Book Library Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45using System; class Book { public string Title; public string Author; public int Pages; // Default constructor public Book() { Title = "Untitled"; Author = "Unknown"; Pages = 0; } // Parameterized constructor public Book(string title, string author, int pages) { Title = title; Author = author; Pages = pages; } // Method to display book info public void Display() { Console.WriteLine($"{Title} by {Author} — {Pages} pages"); } } class Program { static void Main() { Book b1 = new Book("The Alchemist", "Paulo Coelho", 197); Book b2 = new Book("Atomic Habits", "James Clear", 250); Book b3 = new Book(); // using the default constructor Console.WriteLine("Library Books:"); b1.Display(); b2.Display(); b3.Display(); } }
How It Works
- The Book class defines what every book should have
- The constructors let you create books easily
- The Display method prints information cleanly
- Multiple objects represent multiple books in the library
Run this in the DevsCall AI Online Runner to see your digital library appear!
Learn Together with AI
Try prompts like:
- “Add a price field to the Book class and display it.”
- “Create a method that returns whether a book is long or short.”
- “Build a Library class that stores multiple books.”
- “Add an ISBN field with validation.”
AI can help you evolve your project into a full library system.
Practice Time
Create a class called Student with:
- Fields: name, age, grade
- A default constructor
- A parameterized constructor
- A method ShowInfo() that prints details
Expected sample output:
1 2Student 1: Ali, Age 15, Grade 9 Student 2: Sara, Age 16, Grade 10
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25class Student { public string Name; public int Age; public int Grade; public Student() { Name = "Unknown"; Age = 0; Grade = 0; } public Student(string name, int age, int grade) { Name = name; Age = age; Grade = grade; } public void ShowInfo() { Console.WriteLine($"{Name}, Age {Age}, Grade {Grade}"); } }
Frequently Asked Questions
Constructors initialize objects. They set starting values so each object begins in a valid, predictable state.
A default constructor sets pre-defined values, while a parameterized constructor lets you pass custom values when creating an object.
Yes, this is called constructor overloading. It allows different ways to create objects.
They allow you to model real things like books, students, customers, products, or games in an organized, reusable way.
Still have questions?Contact our support team