What Is A Class?

Learn C# Classes in Simple Steps

Beginner Friendly 20 min read Lesson 1 of 11

Welcome! Let's Learn About Classes

A class is a way to organize your code. It's like a form or template that you fill out to create things.

Think of it like this: A class is like a cookie cutter. The cookie cutter itself is the class. The cookies you make are objects.

Step 1: What is a Class?

In Simple Words:

A class is a template or blueprint.

  • It describes what something has (data)
  • It describes what something does (actions)
  • You can make many things from one class
Example: A "Cookie" class describes all cookies. You use it to make chocolate chip, sugar, and oatmeal cookies.
Cookie Cutter

Makes many cookies

🍪 Cookie 1 🍪 Cookie 2 🍪 Cookie 3

One class = Many objects

Step 2: Create Your First Class

Let's create a simple Person class step by step.

Step 2.1: Start the Class
public class Person
{
    // We'll add things here
}

New words:

  • public - Means everyone can use this
  • class - The keyword to create a class
  • Person - The name of our class
Step 2.2: Add Data (Fields)
public class Person
{
    // What does a person have?
    public string Name;  // A name
    public int Age;      // An age
}

Data = Fields

  • string = Text (like "John")
  • int = Whole number (like 25)
  • public = Anyone can see this data
Step 2.3: Add Actions (Methods)
public class Person
{
    // Data (what a person has)
    public string Name;
    public int Age;
    
    // Actions (what a person can do)
    public void SayHello()
    {
        Console.WriteLine($"Hello! My name is {Name}");
    }
    
    public void ShowAge()
    {
        Console.WriteLine($"I am {Age} years old");
    }
}

Actions = Methods

  • void = This method doesn't give back a value
  • () = The method can take inputs (we'll learn later)
  • Console.WriteLine = Prints text to the screen

Step 3: Using Your Class (Program.cs)

Now let's use our Person class to create real people!

Where to write this: In Program.cs (or any other file)
using System;

class Program
{
    static void Main(string[] args)
    {
        // Step 1: Create a Person object
        Person person1 = new Person();
        
        // Step 2: Give the person some data
        person1.Name = "John";
        person1.Age = 25;
        
        // Step 3: Ask the person to do actions
        person1.SayHello();  // Output: Hello! My name is John
        person1.ShowAge();   // Output: I am 25 years old
        
        Console.WriteLine();
        
        // Create another person
        Person person2 = new Person();
        person2.Name = "Jane";
        person2.Age = 30;
        
        person2.SayHello();  // Output: Hello! My name is Jane
        person2.ShowAge();   // Output: I am 30 years old
    }
}
Step 1: Create

new Person() makes a new person

Step 2: Set Data

Set Name and Age for each person

Step 3: Run Actions

Call SayHello() and ShowAge()

Parts of a Class

Fields (Data)

What: Things the class has

public string Name;    // Text data
public int Age;        // Number data
Example: A car has a color, a model, and a speed
Methods (Actions)

What: Things the class does

public void Start()
{
    Console.WriteLine("Car started!");
}
Example: A car can start, stop, and accelerate
Constructor

What: A special method that runs when you create an object

public Person(string name, int age)
{
    Name = name;
    Age = age;
}
Example: new Person("John", 25) - creates with values
Access Modifiers

What: Who can see and use this part

public string Name;    // Everyone can see
private int secret;     // Only this class can see
Example: Your name is public, your password is private

Real Example: A Car Class

Let's create a Car class that makes sense:

Car.cs
using System;

public class Car
{
    // Data: What a car has
    public string Model;
    public string Color;
    public int Speed;
    
    // Constructor: How to make a car
    public Car(string model, string color)
    {
        Model = model;
        Color = color;
        Speed = 0;  // Cars start at 0 speed
    }
    
    // Actions: What a car can do
    public void Start()
    {
        Console.WriteLine($"{Model} is starting...");
    }
    
    public void Accelerate()
    {
        Speed = Speed + 10;
        Console.WriteLine($"{Model} is going {Speed} km/h");
    }
    
    public void Stop()
    {
        Speed = 0;
        Console.WriteLine($"{Model} has stopped");
    }
    
    public void ShowInfo()
    {
        Console.WriteLine($"Model: {Model}, Color: {Color}, Speed: {Speed} km/h");
    }
}
Program.cs (Using the Car)
using System;

class Program
{
    static void Main()
    {
        // Make two cars
        Car car1 = new Car("Toyota", "Red");
        Car car2 = new Car("Honda", "Blue");
        
        // Drive car 1
        car1.Start();
        car1.Accelerate();
        car1.Accelerate();
        car1.ShowInfo();
        car1.Stop();
        
        Console.WriteLine();
        
        // Drive car 2
        car2.Start();
        car2.Accelerate();
        car2.ShowInfo();
        
        Console.WriteLine();
        
        // Both cars are independent!
        car1.ShowInfo();  // Different from car2
        car2.ShowInfo();  // Different from car1
    }
}
Output:
Toyota is starting...
Toyota is going 10 km/h
Toyota is going 20 km/h
Model: Toyota, Color: Red, Speed: 20 km/h
Toyota has stopped

Honda is starting...
Honda is going 10 km/h
Model: Honda, Color: Blue, Speed: 10 km/h

Model: Toyota, Color: Red, Speed: 0 km/h
Model: Honda, Color: Blue, Speed: 10 km/h

Public vs Private - Who Can See What?

public

Everyone can see it

public string Name;  // Anyone can change this
Like: Your name - everyone can know it

private

Only the class can see it

private string password;  // Only this class can see
Like: Your password - only you should know it
Rule of Thumb: Make things private unless you need them to be public. This keeps your data safe!

Properties - Smart Data Control

Properties are like smart fields that can check data before saving it.

Without Property (Not Safe)
public int Age;  // Anyone can set age to -5!

⚠️ Problem: No control over what values are set

With Property (Safe)
private int age;  // Hidden field

public int Age  // Public property
{
    get { return age; }
    set 
    { 
        if (value > 0 && value < 120)  // Check!
            age = value;
        else
            Console.WriteLine("Invalid age!");
    }
}

✅ Safe: Only valid ages can be set!

Benefits of Properties:
  • Check if the data is correct before saving
  • Hide the real data (private field)
  • Add extra logic when data is read or changed

What You Learned Today

Class

A template for making objects

Fields

Data that the class has

Methods

Actions the class can do

Objects

Real things made from a class

Your Turn! Create a Student Class

Follow these steps:
Create the Class

Create a Student class with:

  • Name (string) - public
  • Grade (int) - public
  • SayHello() - prints "Hi, I'm [Name]"
  • ShowGrade() - prints "My grade is [Grade]"
Use the Class

In Program.cs:

  • Create a Student object
  • Set Name and Grade
  • Call both methods
  • Create a second student
Test Your Knowledge - Take Quiz