C# Classes and Objects Explained
Classes and objects are fundamental concepts in C# and object-oriented programming. A class defines the structure and behavior of an object, while an object is an instance created from that class.
What is a Class in C#?
A class is a blueprint that defines data and behavior. It can contain fields, properties, methods, constructors, and other members.
class Car
{
public string Brand { get; set; }
public int Year { get; set; }
public void Start()
{
Console.WriteLine("Car started");
}
}
What is an Object?
An object is an instance of a class. When an object is created, it can store its own values for the properties and use the methods defined by the class.
Car car = new Car();
car.Brand = "Toyota";
car.Year = 2025;
car.Start();
Class vs Object
| Feature | Class | Object |
|---|---|---|
| Meaning | Blueprint | Instance of a class |
| Purpose | Defines structure and behavior | Stores actual data |
| Creation | Declared using class | Created using new |
| Example | Car | new Car() |
C# Properties
Properties provide a convenient way to expose and control data stored by an object.
class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
Creating Multiple Objects
A single class can be used to create many objects. Each object can contain different property values.
Student student1 = new Student();
student1.Name = "Alice";
student1.Age = 20;
Student student2 = new Student();
student2.Name = "Bob";
student2.Age = 22;
Methods in a Class
Methods define actions that an object can perform. They help keep related behavior together with the data it operates on.
class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
Calculator calculator = new Calculator();
int result = calculator.Add(10, 20);
Constructors in C#
A constructor is a special method that runs when an object is created. Constructors are commonly used to initialize object properties.
class Product
{
public string Name { get; set; }
public double Price { get; set; }
public Product(string name, double price)
{
Name = name;
Price = price;
}
}
Product product = new Product("Laptop", 75000);
Access Modifiers
Access modifiers control where classes and their members can be accessed.
- public allows access from other code
- private restricts access to the containing type
- protected allows access within the type and derived types
- internal allows access within the same assembly
Common C# Class Members
| Member | Purpose | Example |
|---|---|---|
| Property | Store and expose data | public string Name { get; set; } |
| Method | Define behavior | void Start() |
| Constructor | Initialize objects | Product(...) |
| Field | Store internal data | private int count |
Example C# Class
class Employee
{
public string Name { get; set; }
public double Salary { get; set; }
public Employee(string name, double salary)
{
Name = name;
Salary = salary;
}
public void DisplayInfo()
{
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Salary: {Salary}");
}
}
Employee employee = new Employee("John", 50000);
employee.DisplayInfo();
Real-World Applications
- User classes in web applications
- Product classes in e-commerce systems
- Employee models in business applications
- Customer objects in banking systems
- Game objects in C# game development
Common Mistakes to Avoid
- Creating classes without clear responsibilities
- Making every member public unnecessarily
- Duplicating code instead of using reusable methods
- Ignoring constructors when initialization is required
- Putting unrelated functionality into one class
Advanced C# OOP Concepts
- Inheritance
- Encapsulation
- Polymorphism
- Abstraction
- Interfaces
- Records
- Generics
Practice Exercises
- Create a Book class
- Add title and author properties
- Create a constructor
- Add a method to display book information
- Create multiple Book objects
- Create a simple BankAccount class
Conclusion
Classes and objects form the foundation of object-oriented programming in C#. By learning how to define classes, create objects, use properties, write methods, and initialize objects with constructors, you can build reusable and maintainable C# applications.