Classes in C# – Learn Classes with Easy Examples
👋 Introduction – Why Do We Need Classes in C#?
Imagine you’re designing a video game. You have a player, enemies, weapons, and levels. Instead of writing separate code for each, you create a template (class) and use it to make multiple game objects.
That’s what classes in C# do! They help you organize code, avoid repetition, and make your program more structured.
Think of a class as a blueprint for an object. Just like a car blueprint defines how every car of that model is built, a class defines how objects are created and behave. 🚗💨
📚 What You Are Going to Learn in This Lesson
✔️ What are classes in C# and why they are important
✔️ How to define and use a class
✔️ Understanding properties and methods inside a class
✔️ Real-world examples of C# classes
✔️ Common mistakes and best practices
Sounds exciting? Let’s jump in! 🎯
📌 What Are Classes in C#?
A class in C# is a blueprint for creating objects. It defines properties (data) and methods (behavior) that objects will have.
📌 Basic Syntax of a Class in C#
class ClassName
{
// Properties (Variables)
// Methods (Functions)
}
class– Keyword to define a classClassName– Name of the class- Inside
{ }– We define properties (variables) and methods (functions)
🎯 Example 1 – Defining and Using a Simple Class
Let’s create a Car class with properties and a method.
using System;
class Car
{
public string brand = "Toyota"; // Property
public void ShowBrand() // Method
{
Console.WriteLine("Car Brand: " + brand);
}
}
class Program
{
static void Main()
{
Car myCar = new Car(); // Creating an object
myCar.ShowBrand(); // Calling method
}
}
🖥️ Output:
Car Brand: Toyota
🔍 Explanation:
- Class
Carhas a propertybrandand a methodShowBrand(). - Inside
Main(), we created an objectmyCarand called the method.
That’s how easy classes in C# make programming! 🚀
🎯 Example 2 – Class with Constructor
A constructor is a method that runs when an object is created. Let’s see how it works.
using System;
class Car
{
public string brand;
public Car(string carBrand) // Constructor
{
brand = carBrand;
}
public void ShowBrand()
{
Console.WriteLine("Car Brand: " + brand);
}
}
class Program
{
static void Main()
{
Car myCar = new Car("Ford"); // Passing value to constructor
myCar.ShowBrand();
}
}
🖥️ Output:
Car Brand: Ford
🔍 Explanation:
- The constructor
Car(string carBrand)initializes thebrandproperty. - When we create an object
new Car("Ford"),"Ford"is passed and stored.
Now, every time you create a Car, you can give it a different brand! 🚗
🎯 Example 3 – Real-World Scenario (Student Class)
Let’s say we are creating a student management system. Every student has a name, age, and grade. Instead of writing separate code for each student, we use a Student class.
using System;
class Student
{
public string name;
public int age;
public char grade;
public Student(string studentName, int studentAge, char studentGrade)
{
name = studentName;
age = studentAge;
grade = studentGrade;
}
public void DisplayInfo()
{
Console.WriteLine("Student: " + name + ", Age: " + age + ", Grade: " + grade);
}
}
class Program
{
static void Main()
{
Student student1 = new Student("Steven", 20, 'A');
student1.DisplayInfo();
}
}
🖥️ Output:
Student: Steven, Age: 20, Grade: A
🔍 Explanation:
- We created a Student class with name, age, and grade properties.
- The constructor initializes values when creating an object.
- The DisplayInfo() method prints student details.
Now, we can create multiple students without rewriting code! 🎓
💡 Why Use Classes in C#?
- Organizes Code – Groups related data and methods together.
- Reusability – Create multiple objects from the same class.
- Encapsulation – Protects data from unwanted modification.
- Scalability – Makes your program expandable and maintainable.
Without classes, managing large programs would be a nightmare! 🏗️
⚠️ Common Mistakes to Avoid
❌ Forgetting to create an object before calling a method
❌ Using static methods when instance methods are needed
❌ Not initializing properties properly
❌ Confusing classes with objects (A class is a template, an object is an instance of that class)
✅ Conclusion
You’ve just mastered classes in C#! 🎉
Now, you know how to define a class, create objects, use constructors, and apply classes to real-world scenarios. This is a huge step toward becoming a C# developer! 🚀
Go ahead, try creating your own classes for a Bank Account, Library Book, or Employee Management System. The more you practice, the better you’ll get! 💡
⏭️ Next What?
Great job! You now understand classes in C#, but what about objects? 🤔
In the next chapter, we’ll dive deep into Objects in C#, learn how they work, and explore real-world coding examples!
Are you ready? Let’s keep going! 🚀
