C# Classes and Objects

In object-oriented programming, classes and objects are fundamental concepts that work together to create organized and reusable code. Let me explain these concepts with clear examples and their outputs.

What is a Class?

A class serves as a blueprint or template for creating objects. Think of it like an architect's building plans - the plans themselves aren't a building, but they define exactly how to construct one. A class describes what characteristics and behaviours all objects of that type will share.

In C#, a class can be defined by using the class keyword. Let's define a class named 'Employee'.


class Employee
{
     //Add your logic here
}

Note: A class can have its own members, called as Class Members.A class can contain one or more constructors, fields, methods, properties, delegates, and events. A class and its members can have access modifiers also such as public, private, protected, and internal, to restrict the access from other parts of project.

What is an Object?

An object is a concrete instance created from a class blueprint. Following our building analogy, if a class is the architectural plan, an object is an actual building constructed from those plans. You can create many different objects from the same class, just like you can build many buildings from the same set of plans.

In C#, an object can be defined by using a new keyword. Let's create an object of class 'Employee' and create its object in Program class.



using System;
public class Employee
{

}
public class Program
{
    public static void Main()
    {
        // Creating an employee object
        Employee emp = new Employee ();
    }
}

Now let's explore these concepts with two examples, starting with a simple one:

Example 1: A Basic Calculator


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

public class Program
{
    public static void Main()
    {
        // Creating a calculator object
        Calculator calc =  new  Calculator ();
        
        // Using the calculator
        calc.Add(5, 3);
    }
}

Output:


Result of 5 + 3 = 8


Now, let's look at a more comprehensive example:

Example 2: Car Management System


using System;

// This is our class - the blueprint
public class Car
{
    //  Methods define what the car can do
    public void Drive()
    {
        Console.WriteLine("The car is moving forward on the road.");
    }

    public void Stop()
    {
        Console.WriteLine("The car comes to a complete stop.");
    }

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

// This is where we use our class to create and use objects
public class Program
{
    public static void Main()
    {
        //  Creating two different car objects from the same blueprint
        Car familyCar = new Car();    // First car object
        Car sportsCar = new Car();    // Second car object

        // Using the family car
        Console.WriteLine("=== Family Car Actions ===");
        familyCar.Drive();  
        familyCar.Honk();   
        familyCar.Stop();   

        Console.WriteLine("\n=== Sports Car Actions ===");
        sportsCar.Drive();  
        sportsCar.Stop();   
    }
}

Output:


=== Family Car Actions ===
The car is moving forward on the road.
Beep! Beep! 
The car comes to a complete stop. 

=== Sports Car Actions ===
The car is moving forward on the road. 
The car comes to a complete stop. 

Understanding the Code

Let's now break down what's happening in our main example:

  1. Class Definition:
    • The Car class defines three behaviours (methods): Drive(), Stop(), and Honk()
    • Each method represents an action that any car object can perform
    • The class serves as a template, defining what all the cars can do

  2. Object Creation:
    • We create two distinct car objects using new Car()
    • familyCar and sportsCar are separate instances of the same class
    • Each object can perform all the actions defined in the Car class

  3. Using Objects:
    • We demonstrate how each car object can perform actions independently
    • The family car performs three actions (Drive, Honk, Stop)
    • The sports car performs two actions (Drive, Stop)
    • Notice how each object maintains its own separate identity

Key Insights

  • One class can create multiple objects, each working independently
  • Objects of the same class share the same methods but can use them separately
  • The new keyword creates a fresh object from the class blueprint
  • Each object can perform any action defined in its class
  • The output shows how different objects can execute the same methods independently

This structure allows us to write organized, reusable code where we define behaviours once in the class and create as many objects as needed, each capable of performing those actions independently.

Summary

The class definition specifies the methods (actions) that its objects can perform, while objects bring these capabilities to life during program execution. This relationship between classes and objects forms the foundation of object-oriented programming in C#, enabling developers to write organized, reusable code that's easy to maintain and scale.