Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Object-Oriented Programming: Classes and Objects in C#

Introduction to Classes and Objects

In Object-Oriented Programming (OOP), a class is a blueprint for creating objects. An object is an instance of a class. Classes define the properties and behaviors (methods) that the objects created from the class will have.

Defining a Class

A class in C# is defined using the class keyword followed by the class name. Here is a simple example:

public class Car {
    public string make;
    public string model;
    public int year;

    public void DisplayInfo() {
        Console.WriteLine($"Make: {make}, Model: {model}, Year: {year}");
    }
}

In this example, the Car class has three properties: make, model, and year. It also has a method DisplayInfo that prints the car's details.

Creating Objects

Once a class is defined, we can create objects from it using the new keyword. Here is how you can create an object of the Car class:

Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Camry";
myCar.year = 2020;
myCar.DisplayInfo();

In this example, we create an object called myCar and set its properties. Finally, we call the DisplayInfo method to display the car's details.

Constructors

A constructor is a special method that is called when an object is instantiated. It is used to initialize the object's properties. Here is an example of a constructor in the Car class:

public class Car {
    public string make;
    public string model;
    public int year;

    // Constructor
    public Car(string make, string model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    public void DisplayInfo() {
        Console.WriteLine($"Make: {make}, Model: {model}, Year: {year}");
    }
}

Now, when we create a new Car object, we can use the constructor to set its properties:

Car myCar = new Car("Toyota", "Camry", 2020);
myCar.DisplayInfo();

Access Modifiers

Access modifiers control the visibility of class members. Common access modifiers in C# include:

  • public: The member is accessible from any code.
  • private: The member is accessible only within the class.
  • protected: The member is accessible within the class and its derived classes.
  • internal: The member is accessible within the same assembly.

Here is an example using different access modifiers:

public class Car {
    public string make;
    private string model;
    protected int year;
    internal string color;

    public void DisplayInfo() {
        Console.WriteLine($"Make: {make}, Model: {model}, Year: {year}, Color: {color}");
    }
}

Properties

Properties provide a way to control the access to the fields of a class. They are defined using get and set accessors. Here is an example:

public class Car {
    private string make;
    private string model;
    private int year;

    public string Make {
        get { return make; }
        set { make = value; }
    }

    public string Model {
        get { return model; }
        set { model = value; }
    }

    public int Year {
        get { return year; }
        set { year = value; }
    }

    public void DisplayInfo() {
        Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");
    }
}

Now, we can access the fields through properties:

Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Camry";
myCar.Year = 2020;
myCar.DisplayInfo();

Methods

Methods define the behaviors of a class. They are functions that belong to a class. Here is an example:

public class Car {
    public string make;
    public string model;
    public int year;

    public void Start() {
        Console.WriteLine("Car started.");
    }

    public void DisplayInfo() {
        Console.WriteLine($"Make: {make}, Model: {model}, Year: {year}");
    }
}

Now, we can call the Start method on a Car object:

Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Camry";
myCar.year = 2020;
myCar.Start();
myCar.DisplayInfo();

Inheritance

Inheritance allows a class to inherit properties and methods from another class. The class that inherits is called the derived class, and the class being inherited from is called the base class. Here is an example:

public class Vehicle {
    public string make;
    public string model;

    public void DisplayInfo() {
        Console.WriteLine($"Make: {make}, Model: {model}");
    }
}

public class Car : Vehicle {
    public int year;

    public void DisplayCarInfo() {
        Console.WriteLine($"Make: {make}, Model: {model}, Year: {year}");
    }
}

Now, the Car class inherits the properties and methods of the Vehicle class:

Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Camry";
myCar.year = 2020;
myCar.DisplayCarInfo();

Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon. It can be achieved through method overriding. Here is an example:

public class Vehicle {
    public virtual void Start() {
        Console.WriteLine("Vehicle started.");
    }
}

public class Car : Vehicle {
    public override void Start() {
        Console.WriteLine("Car started.");
    }
}

Now, when we call the Start method on a Car object, the overridden method is executed:

Vehicle myVehicle = new Vehicle();
myVehicle.Start();

Car myCar = new Car();
myCar.Start();
Output:
Vehicle started.
Car started.

Encapsulation

Encapsulation is the concept of wrapping data and methods into a single unit. It restricts direct access to some of the object's components. Here is an example using private fields and public properties:

public class Car {
    private string make;
    private string model;
    private int year;

    public string Make {
        get { return make; }
        set { make = value; }
    }

    public string Model {
        get { return model; }
        set { model = value; }
    }

    public int Year {
        get { return year; }
        set { year = value; }
    }

    public void DisplayInfo() {
        Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");
    }
}

This ensures that the fields are not directly accessible and can only be accessed through the properties.

Conclusion

Understanding classes and objects is fundamental to grasping the concepts of Object-Oriented Programming. By defining classes, creating objects, using constructors, and applying principles like inheritance, polymorphism, and encapsulation, you can create robust and reusable code in C#.