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.

CSHARP
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.

CSHARP
Car car = new Car();
car.Brand = "Toyota";
car.Year = 2025;

car.Start();

Class vs Object

FeatureClassObject
MeaningBlueprintInstance of a class
PurposeDefines structure and behaviorStores actual data
CreationDeclared using classCreated using new
ExampleCarnew Car()

C# Properties

Properties provide a convenient way to expose and control data stored by an object.

CSHARP
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.

CSHARP
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.

CSHARP
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.

CSHARP
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

MemberPurposeExample
PropertyStore and expose datapublic string Name { get; set; }
MethodDefine behaviorvoid Start()
ConstructorInitialize objectsProduct(...)
FieldStore internal dataprivate int count

Example C# Class

CSHARP
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.

Note: Note: Keep classes focused on a clear responsibility and use appropriate access modifiers to protect internal data.