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
Car
has a propertybrand
and a methodShowBrand()
. - Inside
Main()
, we created an objectmyCar
and 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 thebrand
property. - 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! π