Loading...

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:

csharp
6 lines
|
22/ 500 tokens
1
2
3
4
5
6
class Book
{
    public string title;
    public string author;
    public int pages;
}
Code Tools

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.

csharp
4 lines
|
24/ 500 tokens
1
2
3
4
Book b1 = new Book();
b1.title = "The Alchemist";
b1.author = "Paulo Coelho";
b1.pages = 200;
Code Tools

Now you have a real book stored in memory.

Fields (Variables Inside a Class)

Fields store information about an object.

csharp
3 lines
|
16/ 500 tokens
1
2
3
public string title;
public string author;
public double price;
Code Tools

Each object gets its own copy.

Methods (Actions of the Object)

A method describes behavior, what the object can do.

csharp
4 lines
|
19/ 500 tokens
1
2
3
4
public void DisplayInfo()
{
    Console.WriteLine($"{title} by {author}");
}
Code Tools

Constructors

A constructor sets initial values when the object is created.

Default Constructor

Executes automatically and sets basic defaults.

csharp
6 lines
|
20/ 500 tokens
1
2
3
4
5
6
public Book()
{
    title = "Unknown";
    author = "Unknown";
    pages = 0;
}
Code Tools

Parameterized Constructor

Lets you pass values when creating the object.

csharp
6 lines
|
22/ 500 tokens
1
2
3
4
5
6
public Book(string t, string a, int p)
{
    title = t;
    author = a;
    pages = p;
}
Code Tools

Now creating a book becomes:

csharp
1 lines
|
13/ 500 tokens
1
Book b1 = new Book("1984", "George Orwell", 328);
Code Tools

Let’s Build the Book Library Model

csharp
45 lines
|
228/ 500 tokens
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
45
using 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();
    }
}
Code Tools

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:

csharp
2 lines
|
17/ 500 tokens
1
2
Student 1: Ali, Age 15, Grade 9
Student 2: Sara, Age 16, Grade 10
Code Tools

Example Solution

csharp
25 lines
|
103/ 500 tokens
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
class 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}");
    }
}
Code Tools

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