09. Classes and Objects in C#
Level: Beginner
- What a class is
- What an object is
- Why classes are needed
- Create a
Studentclass - Add properties and methods
- Create objects from a class
- Use constructors to set required data
So far, you stored student data using separate variables:
string name = "Sahasra Kumar";
int rollNumber = 101;
string className = "10-A";
double percentage = 84.8;
This works for one student.
But a real School Management System has many students. Keeping separate variables for every student becomes messy.
The Problem: Too Many Separate Variables
Imagine three students:
string student1Name = "Sahasra";
int student1Roll = 101;
string student2Name = "Priya";
int student2Roll = 102;
string student3Name = "Arjun";
int student3Roll = 103;
This is hard to manage.
Better:
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
For this, we need a class.
What is a Class?
A class is a blueprint.
It describes what data and actions an object should have.
Student class says:
- Student has Name
- Student has RollNumber
- Student has ClassName
- Student has Percentage
- Student can PrintInfo()
What is an Object?
An object is a real item created from a class.
Class = Student blueprint
Object = Sahasra Kumar student record
Object = Priya Sharma student record
Quick Definitions
- Class - Blueprint describing properties and methods
- Object - Real item created from a class (also called "instance")
- Property - Data stored in an object (like Name, RollNumber)
- Method - Action an object can perform
- Constructor - Code running when object is created
- Encapsulation - Bundling data and methods together
- Instance - Another word for object
- Get/Set - Ways to read and change property values
What Does { get; set; } Mean?
In a property, { get; set; } allows reading and changing values.
public string Name { get; set; } = "";
Breakdown:
get= read the property:Console.WriteLine(student.Name);set= change the property:student.Name = "Sahasra";= ""= default value (empty string)
Without { get; set; }, you cannot read or change the property.
Think of it like a locked/unlocked locker:
{ get; }= can read only (read-only){ set; }= can change only (write-only){ get; set; }= can read AND change (normal usage)
Step 1: Create a Simple Class
public class Student
{
public string Name { get; set; } = "";
public int RollNumber { get; set; }
public string ClassName { get; set; } = "";
public double Percentage { get; set; }
}
This class says every student object can store:
- Name
- Roll number
- Class name
- Percentage
Step 2: Create an Object
Student student = new Student();
student.Name = "Sahasra Kumar";
student.RollNumber = 101;
student.ClassName = "10-A";
student.Percentage = 84.8;
Meaning:
Student student = new Student();
Create one real student object from the Student class.
Step 3: Read Object Data
Console.WriteLine(student.Name);
Console.WriteLine(student.RollNumber);
Console.WriteLine(student.ClassName);
Console.WriteLine(student.Percentage);
Step 4: Add a Method Inside Class
A method inside a class is an action the object can do.
public class Student
{
public string Name { get; set; } = "";
public int RollNumber { get; set; }
public string ClassName { get; set; } = "";
public double Percentage { get; set; }
public void PrintInfo()
{
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Roll Number: {RollNumber}");
Console.WriteLine($"Class: {ClassName}");
Console.WriteLine($"Percentage: {Percentage}");
}
}
Call it:
student.PrintInfo();
Step 5: Add Calculated Properties
Some values can be calculated from existing data.
public bool HasPassed
{
get { return Percentage >= 35; }
}
public string Result
{
get { return HasPassed ? "Pass" : "Fail"; }
}
Now you can write:
Console.WriteLine(student.Result);
Step 6: Constructor
A constructor runs when an object is created.
It helps set required values immediately.
public class Student
{
public string Name { get; set; } = "";
public int RollNumber { get; set; }
public string ClassName { get; set; } = "";
public double Percentage { get; set; }
public Student(string name, int rollNumber, string className)
{
Name = name;
RollNumber = rollNumber;
ClassName = className;
}
}
Create object:
Student student = new Student("Sahasra Kumar", 101, "10-A");
student.Percentage = 84.8;
Full Example: Student Class
Class Terms
| Term | Meaning |
|---|---|
| Class | Blueprint |
| Object | Real item created from class |
| Property | Data stored in object |
| Method | Action object can do |
| Constructor | Runs when object is created |
When You'll Use This in SMS
Classes are foundation of SMS. Every entity is a class.
Student class:
- Properties: Name, RollNumber, ClassName, Percentage
- Methods: PrintInfo(), GetGrade(), IsEligible()
Teacher class:
- Properties: Name, EmployeeCode, Subject, Salary
- Methods: PrintInfo(), UpdateSalary()
Fee class:
- Properties: StudentId, Amount, PaidOn, Status
- Methods: MarkAsPaid(), CalculateBalance()
Exam class:
- Properties: SubjectId, ExamDate, MaxMarks
- Methods: RecordResult(), GenerateReport()
Real impact: Each class handles one entity. Changing Student doesn't affect Teacher. Easy to manage 10,000 students.
Try This Now
Run the Student class example. Then create your own:
- Create Teacher class with Name, EmployeeCode, Subject, Salary
- Add method: PrintInfo() showing all details
- Create objects: Teacher teacher1 = new Teacher()
- Set properties and call PrintInfo()
See how one Teacher class template creates many teacher objects.
Classes and objects explained: creating classes, properties, constructors, objects. Video coming soon. Subscribe to NexCoding YouTube for updates.
Common Mistakes
Mistake 1: Forgetting new
Wrong:
Student student;
student.Name = "Sahasra";
Correct:
Student student = new Student("Sahasra", 101, "10-A");
Mistake 2: Creating class but not creating object
A class is only a blueprint. You must create an object to store real data.
Mistake 3: Public fields instead of properties
Prefer:
public string Name { get; set; } = "";
Avoid:
public string name;
Mistake 4: Making class do too many jobs
Student should handle student data. Do not put teacher, fees, and database logic inside the same class.
Best Practices
- Use PascalCase for class names:
Student,Teacher. - Use properties for data.
- Use constructors for required data.
- Keep one class focused on one thing.
- Use methods for actions related to the object.
- Give properties meaningful names.
Practice Task
Create a Teacher class with:
- Name
- EmployeeCode
- Subject
- ExperienceYears
- PrintInfo method
Create two teacher objects and print their details.
Quick Revision
| Question | Answer |
|---|---|
| What is a class? | Blueprint |
| What is an object? | Real item created from class |
| What is a property? | Data inside object |
| What is a method? | Action inside class |
| What is a constructor? | Code that runs when object is created |
A class is a blueprint. An object is a real instance created from that blueprint.
Example:
public class Student { }
Student Sahasra = new Student();
A constructor is a special method that runs when an object is created. It is used to set required data.
Properties store object data and provide a clean way to read and write values.
A method inside a class is an action the object can perform, like PrintInfo().
It keeps code simple, readable, and easy to maintain.
Use ChatGPT, Claude, or Copilot to go deeper on C# classes and objects. Try these prompts:
"Explain class and object using a school example""Give me 5 practice tasks for classes and objects""Explain constructor and properties in simple words""Quiz me with 5 beginner questions about classes"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.