C# OOP

Object-Oriented Programming in C#.

* OOP stands for Object-Oriented Programming. It's a programming paradigm that revolves around the concept of "objects," which are instances of classes. Learn more in-depth concepts about OOP from the C++ chapter.

OOP in C#

Object-Oriented Programming (OOP) in C# is a fundamental concept that allows developers to create modular, reusable, and maintainable code by organizing it into objects that interact with each other. Here's an in-depth overview of OOP principles in C#:

Classes and Objects: Everything revolves around classes and objects. A class is a blueprint for creating objects, and an object is an instance of a class. Classes define the properties (attributes) and behaviors (methods) of objects.

class Car {
    public string Make { get; set; }
    public string Model { get; set; }

    public void Drive() {
        Console.WriteLine("Driving...");
    }
}

Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Camry";
myCar.Drive();

Encapsulation: Encapsulation is the bundling of data (attributes) and methods (behaviors) that operate on that data within a single unit, i.e., a class. It hides the internal state of an object and only exposes the necessary functionalities.

class Person {
    private string name;

    public string Name {
        get { return name; }
        set { name = value; }
    }

    public void DisplayInfo() {
        Console.WriteLine($"Name: {Name}");
    }
}

Inheritance: Inheritance allows a class (subclass/derived class) to inherit the properties and behaviors of another class (base class). It promotes code reuse and establishes a hierarchical relationship between classes.

class Person {
    private string name;

    public string Name {
        get { return name; }
        set { name = value; }
    }

    public void DisplayInfo() {
        Console.WriteLine($"Name: {Name}");
    }
}

Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables methods to behave differently based on the object they are called on. There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding).

class Shape {
    public virtual void Draw() {
        Console.WriteLine("Drawing a shape...");
    }
}

class Circle : Shape {
    public override void Draw() {
        Console.WriteLine("Drawing a circle...");
    }
}

class Rectangle : Shape {
    public override void Draw() {
        Console.WriteLine("Drawing a rectangle...");
    }
}

Shape shape = new Circle();
shape.Draw();  // Output: Drawing a circle
shape = new Rectangle();
shape.Draw();  // Output: Drawing a rectangle

Abstraction: Abstraction focuses on hiding the complex implementation details and showing only the essential features of an object. It is achieved through abstract classes and interfaces.

abstract class Shape {
    public abstract void Draw();
}

class Circle : Shape {
    public override void Draw() {
        Console.WriteLine("Drawing a circle...");
    }
}

interface IShape {
    void Draw();
}

class Rectangle : IShape {
    public void Draw() {
        Console.WriteLine("Drawing a rectangle...");
    }
}

Interfaces: Interfaces define a contract that classes can implement. They contain only the signatures of methods, properties, events, or indexers. A class can implement multiple interfaces but can inherit from only one class.

interface IAnimal {
    void Eat();
    void Sleep();
}

class Dog : IAnimal {
    public void Eat() {
        Console.WriteLine("Dog is eating...");
    }

    public void Sleep() {
        Console.WriteLine("Dog is sleeping...");
    }
}

These are the core concepts of OOP in C#. Understanding and effectively utilizing these concepts can lead to well-structured, maintainable, and scalable code.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.