C# OOP Overview


Object-Oriented Programming (OOP) in C# is a programming paradigm that organizes software design around data, or objects, rather than functions and logic. OOP focuses on using objects to represent real-world entities, allowing for easier management of complex systems by bundling data and behaviors into reusable components.

Key Concepts of OOP in C#

  1. Classes and Objects

    • Class: A class is a blueprint for creating objects (instances). It defines properties (data) and methods (behavior) that the objects will have.
    • Object: An object is an instance of a class. It holds the actual data and can perform actions defined by its class’s methods.

    Example:

    class Car { // Properties public string Make { get; set; } public string Model { get; set; } public int Year { get; set; } // Method public void Start() { Console.WriteLine("The car has started."); } } class Program { static void Main() { // Creating an object (instance) of the Car class Car myCar = new Car { Make = "Toyota", Model = "Corolla", Year = 2020 }; // Accessing properties and methods myCar.Start(); // Output: The car has started. } }
  2. Encapsulation

    • Encapsulation is the concept of bundling data (fields) and methods that operate on the data into a single unit (class). It also involves restricting direct access to some of an object’s components, which is typically done by using access modifiers (private, public, protected).

    Example:

    class BankAccount { // Private field private decimal balance; // Public method to access the private field public void Deposit(decimal amount) { balance += amount; } // Public method to get the balance public decimal GetBalance() { return balance; } } class Program { static void Main() { BankAccount account = new BankAccount(); account.Deposit(1000); Console.WriteLine(account.GetBalance()); // Output: 1000 } }
  3. Inheritance

    • Inheritance allows a class (derived class) to inherit fields and methods from another class (base class), promoting code reuse. The derived class can also add its own methods or override existing ones.

    Example:

    class Animal { public void Eat() { Console.WriteLine("The animal is eating."); } } class Dog : Animal { public void Bark() { Console.WriteLine("The dog is barking."); } } class Program { static void Main() { Dog myDog = new Dog(); myDog.Eat(); // Inherited from Animal class myDog.Bark(); // Specific to Dog class } }
  4. Polymorphism

    • Polymorphism allows one method to behave differently based on the object calling it. In C#, this is achieved through method overriding and interfaces.

    Method Overriding Example:

    class Animal { public virtual void Speak() { Console.WriteLine("The animal makes a sound."); } } class Dog : Animal { public override void Speak() { Console.WriteLine("The dog barks."); } } class Program { static void Main() { Animal myAnimal = new Dog(); myAnimal.Speak(); // Output: The dog barks. } }
    • In this example, the Speak method behaves differently depending on whether it’s called on an Animal or a Dog object.
  5. Abstraction

    • Abstraction is the concept of hiding the complex implementation details and exposing only the essential features of an object. This is often achieved through abstract classes or interfaces.

    Example:

    abstract class Shape { // Abstract method (no implementation) public abstract double GetArea(); } class Circle : Shape { private double radius; public Circle(double radius) { this.radius = radius; } // Implementing the abstract method public override double GetArea() { return Math.PI * radius * radius; } } class Program { static void Main() { Shape shape = new Circle(5); Console.WriteLine($"Area of the circle: {shape.GetArea()}"); // Output: Area of the circle: 78.54 } }

Pillars of OOP in C#

  1. Encapsulation: Ensures that an object’s internal state cannot be altered directly, enforcing controlled access through methods.

  2. Abstraction: Simplifies complex reality by modeling classes appropriate to the problem domain, exposing only the necessary parts to the user.

  3. Inheritance: Allows code reuse and the creation of a class hierarchy, where a derived class can inherit functionality from a base class and add or modify behavior.

  4. Polymorphism: Enables a single function or method to behave differently based on the type of object that calls it, often using method overriding or interfaces.

Access Modifiers in OOP

  • public: The member is accessible from anywhere.
  • private: The member is accessible only within the class it is declared in.
  • protected: The member is accessible within the class and by derived classes.
  • internal: The member is accessible within the same assembly.

Example OOP Code Combining Concepts

using System; class Vehicle { public string Make { get; set; } public string Model { get; set; } public Vehicle(string make, string model) { Make = make; Model = model; } public virtual void Drive() { Console.WriteLine("Driving the vehicle"); } } class Car : Vehicle { public int Year { get; set; } public Car(string make, string model, int year) : base(make, model) { Year = year; } public override void Drive() { Console.WriteLine($"Driving the car: {Make} {Model}, Year: {Year}"); } } class Program { static void Main() { Vehicle myVehicle = new Vehicle("Generic", "Vehicle"); myVehicle.Drive(); // Output: Driving the vehicle Car myCar = new Car("Toyota", "Camry", 2021); myCar.Drive(); // Output: Driving the car: Toyota Camry, Year: 2021 } }

Conclusion

Object-Oriented Programming in C# provides a powerful way to design and structure code using real-world concepts. Through encapsulation, inheritance, polymorphism, and abstraction, C# enables developers to build maintainable, reusable, and flexible code, leading to better software design.