Lesson: C# Object-Oriented Programming (OOP)

What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which can contain both data and methods. The four main principles of OOP are:

Class and Object

A class is a blueprint for creating objects, and an object is an instance of a class.

Here's how a class is defined in C#:

class Car {
    public string make;
    public string model;

    public void Drive() {
        Console.WriteLine("The car is driving.");
    }
}

In the above example, `Car` is a class with two attributes (`make`, `model`) and one method (`Drive`).

Creating Objects

To create an object, we use the `new` keyword followed by the class name:

Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Corolla";
myCar.Drive();

This code creates an instance of the `Car` class and assigns values to its properties, then calls its method to simulate the car driving.

Interactive Example: Create and Use a Class

Enter the car's make and model, and simulate driving the car:



Car Information and Status:

Quiz: Test Your Knowledge on C# OOP

1. What is the primary purpose of Object-Oriented Programming (OOP)?




2. What does "encapsulation" mean in OOP?




3. Which of the following is an example of polymorphism in OOP?




4. What is the difference between a class and an object?




5. Which of the following is true about inheritance in OOP?