CodeGym /Courses /C# SELF /The Concept of Class and Object

The Concept of Class and Object

C# SELF
Level 16 , Lesson 0
Available

1. Introduction

As you've probably heard, a class is a template, and an object is its instance.
But now it's time to dig deeper into classes and objects!

Imagine you're a design engineer at a car factory. Instead of building each car by hand, you create a blueprint—a universal instruction that lets you assemble a bunch of cars of the same type. This blueprint says a car should have an engine, wheels, a body, a color, and what functions it can perform.

This blueprint, in programming, is called a class.

Now imagine the factory uses this blueprint to produce a batch of cars. Each one is real, driving on the road, with its own number, color, and even some custom settings. But they're all built from the same blueprint.

These real cars, in programming, are called objects (or class instances).

So, let's remember:

  • Class — it's like a blueprint, a template, a scheme. It describes what data something can have (like brand, color, number of doors) and what that thing can do (like drive, brake, honk). A class is just a description, it doesn't take up much memory by itself and isn't something "alive".
  • Object — it's a specific "car" built from the class blueprint. It's a real, "living" thing in your program, which takes up space in memory, has its own unique values for data (its own number, color), and can perform actions described in the class.

Sounds a bit abstract? Don't worry, we'll break it all down right now.

2. What is a class?

A class is a custom data type that describes what objects of this type should look like: what characteristics (state/properties/fields) they have and what actions you can perform with them (behavior/methods/functions).

A class defines:

  • Fields (variables) — data stored in the object.
  • Methods (functions) — actions the object can perform.
  • Properties — special methods for accessing data.
  • Events — notification mechanisms (more on these later).
  • Constructors — special methods for creating objects.

public class Person
{
    // Fields (data)
    public string Name;
    public int Age;

    // Method (action)
    public void SayHello()
    {
        Console.WriteLine($"Hi! My name is {Name}, I'm {Age} years old.");
    }
}

In this example, we declared a class called Person. It has two fields (Name and Age) and a method SayHello(). That's all you need for a super basic class.

3. What is an object?

An object is an instance of a class, meaning a specific thing created in memory from the "blueprint" (the class).

When you create a variable of type Person, you get an object—with unique values for each field. Not just a person, but, say, a person named Johnny who's 12 years old. And when he greets us (that is, when you call the SayHello() method), he "tells" us info about himself.


// Creating an object of the Person class
Person person1 = new Person();
person1.Name = "Alisa";
person1.Age = 28;
person1.SayHello(); // => Hi! My name is Alisa, I'm 28 years old.

// Creating another "person"
Person person2 = new Person();
person2.Name = "Bob";
person2.Age = 32;
person2.SayHello(); // => Hi! My name is Bob, I'm 32 years old.

Each object isn't just a copy: it's an independent thing with its own data (in our example—names and ages).

4. The main difference between a class and an object

The main difference:

  • Class — "blueprint", instruction, data type.
  • Object — a specific representative of that type; a real "house" built from the plan; a "car" produced from the blueprint.

You can create as many objects of one class as you want. For example, if you have a Person class, you can create a million "people" in your program.

Why is this convenient and important?

Let's try to imagine a program without classes. If you had a list of students, you'd have to store names, ages, grades, and other data in separate arrays for each parameter:

string[] studentNames = { "Sergey", "Grisha", "Masha" };
int[] studentAges = { 20, 19, 21 };

// If you need to add another property, that's another array.
// Trying to swap a couple of students around becomes a total nightmare!

Using classes, your data doesn't get scattered across different arrays, but is logically grouped:

public class Student
{
    public string Name;
    public int Age;
}

Student[] students = new Student[3];
students[0] = new Student { Name = "Sergey", Age = 20 };
students[1] = new Student { Name = "Grisha", Age = 19 };
students[2] = new Student { Name = "Masha", Age = 21 };

Way clearer and more convenient! Plus, now you can add methods to each student (like "say hello" or "calculate average grade").

Picture: the connection between class and object

+------------------+          +----------------+
|   class Person   |=========>|  person1:      | 
| (blueprint/type) |          |  Name = "Alisa"|
|                  |         /|  Age = 28      |
+------------------+        / +----------------+
                          /
+------------------------/----------------------+
                        /
                  +----------------+
                  |  person2:      |
                  |  Name = "Bob"  |
                  |  Age = 32      |
                  +----------------+

5. Where are classes and objects used in real life?

Classes and objects are the foundation for any C# app. Everything you see around you in .NET: windows, buttons, forms, arrays, collections, database connections, logs—they're all objects created from certain classes.

Examples:

  • The Button class is the instruction for what a button should look like on the screen. Every time you create a new button—that's an object of the class.
  • The Stream class (from the input/output system): you get an object that works with files.
  • Your future User class for storing user info—again, same template idea.

Quick note about names, modifiers, and conventions

  • For classes, it's standard to use names starting with a capital letter, like Person, Order, Product.
  • For variables—start with a lowercase letter, like person, user1, orderList.
  • Most of the time, you make a class public—so it's available everywhere in your project.
2
Task
C# SELF, level 16, lesson 0
Locked
Creating a Simple Class and Object
Creating a Simple Class and Object
2
Task
C# SELF, level 16, lesson 0
Locked
Several objects of the same class
Several objects of the same class
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION