Skip to main content

09. Classes and Objects in C#

Level: Beginner

ℹ️ What You'll Learn
  • What a class is
  • What an object is
  • Why classes are needed
  • Create a Student class
  • 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

💻 Try It — Console App
💡 Paste into Program.cs and press F5⌥ GitHub
Student Sahasra = new Student("Sahasra Kumar", 101, "10-A");
Sahasra.Percentage = 84.8;
Sahasra.PrintInfo();

Student priya = new Student("Priya Sharma", 102, "10-A");
priya.Percentage = 91.2;
priya.PrintInfo();

public class Student
{
public string Name { get; set; } = "";
public int RollNumber { get; set; }
public string ClassName { get; set; } = "";
public double Percentage { get; set; }

public bool HasPassed
{
get { return Percentage >= 35; }
}

public string Result
{
get { return HasPassed ? "Pass" : "Fail"; }
}

public Student(string name, int rollNumber, string className)
{
Name = name;
RollNumber = rollNumber;
ClassName = className;
}

public void PrintInfo()
{
Console.WriteLine("=== Student Details ===");
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Roll Number: {RollNumber}");
Console.WriteLine($"Class: {ClassName}");
Console.WriteLine($"Percentage: {Percentage:F2}");
Console.WriteLine($"Result: {Result}");
Console.WriteLine();
}
}

Class Terms

TermMeaning
ClassBlueprint
ObjectReal item created from class
PropertyData stored in object
MethodAction object can do
ConstructorRuns 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:

  1. Create Teacher class with Name, EmployeeCode, Subject, Salary
  2. Add method: PrintInfo() showing all details
  3. Create objects: Teacher teacher1 = new Teacher()
  4. Set properties and call PrintInfo()

See how one Teacher class template creates many teacher objects.


ℹ️ Video Tutorial

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

  1. Use PascalCase for class names: Student, Teacher.
  2. Use properties for data.
  3. Use constructors for required data.
  4. Keep one class focused on one thing.
  5. Use methods for actions related to the object.
  6. 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

QuestionAnswer
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

🎯 Q1: What is the difference between class and object?

A class is a blueprint. An object is a real instance created from that blueprint.

Example:

public class Student { }
Student Sahasra = new Student();
🎯 Q2: What is a constructor?

A constructor is a special method that runs when an object is created. It is used to set required data.

🎯 Q3: Why do we use properties?

Properties store object data and provide a clean way to read and write values.

🎯 Q4: What is a method inside a class?

A method inside a class is an action the object can perform, like PrintInfo().

🎯 Q5: Why should a class do one job?

It keeps code simple, readable, and easy to maintain.


🤖Use AI to Learn Faster
⚠️ Important for beginners: Do NOT use AI to write your code yet. Type every example yourself. Your brain learns by doing, not by reading AI output. Use AI only to explain and quiz you — not to code for you. Once you have strong fundamentals, AI becomes a powerful productivity tool for repetitive tasks.

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.

Next Article

Properties - get and set ->

nexcoding.in