C# Classes and Objects

In C# and object-oriented programming, classes and objects are the building blocks of almost every application.

Whether you are developing a simple console application or a large enterprise system, everything revolves around how you design your classes and create objects.

Understanding these concepts properly will help you write clean, reusable, and scalable code.


What is a Class in C#?

A class is a blueprint or template used to create objects.

It defines what properties (data) and behaviors (methods) an object will have.

Think of a class like a design of a house — it defines structure, but it is not the actual house.

class Employee
{
    // Class members will be defined here
}

This class currently does nothing, but it acts as a template for creating Employee objects.


Class Members in C#

A class can contain different types of members that define its structure and behavior:

  • Fields – Store data
  • Methods – Define actions
  • Constructors – Initialize objects
  • Properties – Controlled access to fields
  • Events and Delegates – Advanced features

Access to these members can be controlled using access modifiers like public, private, and protected.


What is an Object in C#?

An object is an instance of a class.

If a class is a blueprint, then an object is the actual implementation created from that blueprint.

You can create multiple objects from a single class.

using System;

class Employee
{
}

class Program
{
    static void Main()
    {
        Employee emp = new Employee();
    }
}

The new keyword creates a new object in memory.


Example 1: Calculator (Basic Example)

using System;

class Calculator
{
    public void Add(int a, int b)
    {
        Console.WriteLine($"Result: {a + b}");
    }
}

class Program
{
    static void Main()
    {
        Calculator calc = new Calculator();
        calc.Add(5, 3);
    }
}

Output

Result: 8

This example shows how a class defines behavior and an object executes it.


Example 2: Car System (Multiple Objects)

using System;

class Car
{
    public void Drive()
    {
        Console.WriteLine("Car is moving");
    }

    public void Stop()
    {
        Console.WriteLine("Car stopped");
    }

    public void Honk()
    {
        Console.WriteLine("Beep Beep");
    }
}

class Program
{
    static void Main()
    {
        Car car1 = new Car();
        Car car2 = new Car();

        car1.Drive();
        car1.Honk();

        car2.Drive();
        car2.Stop();
    }
}

Output

Car is moving
Beep Beep
Car is moving
Car stopped

Each object works independently even though they are created from the same class.


Example 3: Adding Properties (Realistic Scenario)

Let’s make our class more realistic by adding data.

class Employee
{
    public string Name;
    public int Age;

    public void Display()
    {
        Console.WriteLine(Name + " - " + Age);
    }
}

class Program
{
    static void Main()
    {
        Employee emp1 = new Employee();
        emp1.Name = "John";
        emp1.Age = 30;

        emp1.Display();
    }
}

This shows how objects can hold their own data.


How Memory Works (Important Concept)

When you create an object using new, memory is allocated in the heap.

The variable holds a reference to that object.

Employee emp = new Employee();

Here:

  • Object → stored in heap
  • emp → holds reference to that object

This is why classes are called reference types.


Object Initialization (Cleaner Syntax)

C# provides a cleaner way to initialize objects.

Employee emp = new Employee
{
    Name = "Roni",
    Age = 28
};

This improves readability and reduces code.


Common Mistakes Developers Make

  • Forgetting to create object using new
  • Trying to access members without object instance
  • Confusing class with object
  • Not initializing properties

Best Practices

  • Keep classes focused on a single responsibility
  • Use meaningful names for classes and objects
  • Encapsulate data using properties
  • Avoid creating unnecessary objects

Key Insights

  • A class is a blueprint, object is its instance
  • One class can create multiple objects
  • Objects work independently
  • Classes define structure, objects execute behavior

Summary

Classes and objects form the foundation of object-oriented programming in C#.

A class defines what an object will look like and what it can do, while objects bring that definition to life.

By mastering these concepts, you can build scalable, maintainable, and real-world applications effectively.

In the next chapter, we will explore Constructors in C# and how objects are initialized.