What Is A Class?
Learn C# Classes in Simple Steps
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.
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
Cookie Cutter
↓ Makes many cookies ↓
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!
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
Methods (Actions)
What: Things the class does
public void Start()
{
Console.WriteLine("Car started!");
}
Constructor
What: A special method that runs when you create an object
public Person(string name, int age)
{
Name = name;
Age = age;
}
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
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
}
}
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
private
Only the class can see it
private string password; // Only this class can see
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!
- 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) - publicGrade(int) - publicSayHello()- 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
// Student.cs
using System;
public class Student
{
// Data
public string Name;
public int Grade;
// Actions
public void SayHello()
{
Console.WriteLine($"Hi, I'm {Name}");
}
public void ShowGrade()
{
Console.WriteLine($"My grade is {Grade}");
}
}
// Program.cs
class Program
{
static void Main()
{
// Student 1
Student student1 = new Student();
student1.Name = "Alice";
student1.Grade = 90;
student1.SayHello(); // Output: Hi, I'm Alice
student1.ShowGrade(); // Output: My grade is 90
Console.WriteLine();
// Student 2
Student student2 = new Student();
student2.Name = "Bob";
student2.Grade = 85;
student2.SayHello(); // Output: Hi, I'm Bob
student2.ShowGrade(); // Output: My grade is 85
}
}