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 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 class
  • ClassName – 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 property brand and a method ShowBrand().
  • Inside Main(), we created an object myCar 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 the brand 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#?

  1. Organizes Code – Groups related data and methods together.
  2. Reusability – Create multiple objects from the same class.
  3. Encapsulation – Protects data from unwanted modification.
  4. 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! πŸš€

Leave a Comment

Share this Doc

Classes

Or copy link