Introduction

Object-Oriented Programming (OOP) is a programming paradigm that is widely used in modern software development. C# is an object-oriented programming language developed by Microsoft, which supports various OOP concepts like encapsulation, inheritance, and polymorphism. In this article, we will discuss the fundamentals of OOP in C#, including the concepts of classes, objects, inheritance, polymorphism, and more.

Classes and Objects:

Classes and objects are the basic building blocks of object-oriented programming. A class is a blueprint or a template for creating objects, while an object is an instance of a class. In C#, classes are defined using the “class” keyword, and objects are created using the “new” keyword. Here’s an example of a class in C#:

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

In this example, we define a class called “Person” with two properties: “Name” and “Age”. We can create objects of this class by using the “new” keyword:

Person person = new Person();
person.Name = "John";
person.Age = 30;

Encapsulation:

Encapsulation is the concept of hiding the internal details of an object and exposing only the necessary information through a public interface. In C#, encapsulation is achieved by using access modifiers such as public, private, and protected. Here’s an example:

class BankAccount
{
    private decimal balance;

    public decimal GetBalance()
    {
        return balance;
    }

    public void Deposit(decimal amount)
    {
        balance += amount;
    }

    public void Withdraw(decimal amount)
    {
        if (balance >= amount)
        {
            balance -= amount;
        }
        else
        {
            throw new Exception("Insufficient balance");
        }
    }
}

In this example, we define a class called “BankAccount” with a private member variable called “balance”. We also define three public methods: “GetBalance”, “Deposit”, and “Withdraw”, which can be used to interact with the balance. By making the “balance” variable private, we ensure that it cannot be accessed directly from outside the class, thus encapsulating it.

Inheritance:

Inheritance is the concept of creating a new class from an existing class, inheriting its properties and methods. In C#, inheritance is achieved using the “class” keyword, followed by a colon and the name of the base class. Here’s an example:

class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Barking...");
    }
}

In this example, we define a class called “Animal” with a method called “Eat”. We then define another class called “Dog”, which inherits from “Animal” and adds a new method called “Bark”. The “Dog” class now has access to the “Eat” method of the “Animal” class, as well as its own “Bark” method.

Polymorphism:

Polymorphism is the concept of using the same name for multiple methods with different implementations. In C#, polymorphism is achieved using method overriding and method overloading. Method overriding is the concept of providing a different implementation of a method in a derived class, while method overloading is the concept of providing multiple versions of a method with different parameters in the same class. Here’s an example:

class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Animal is making a sound...");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
}

public void MakeSound(string message)
{
    Console.WriteLine(message);
}

In this example, we define a class called “Animal” with a virtual method called “MakeSound”. We then define another class called “Dog”, which overrides the “MakeSound” method and provides a different implementation. We also define an overloaded version of the “MakeSound” method in the “Dog” class, which takes a string parameter and outputs the message to the console.

Conclusion:

In conclusion, object-oriented programming is a fundamental concept in modern software development, and C# is a powerful programming language that supports various OOP concepts such as classes, objects, encapsulation, inheritance, polymorphism, and more. By mastering these concepts, developers can write more organized, modular, and reusable code, which is easier to maintain and extend. We hope this article has provided you with a solid understanding of OOP in C# and its practical applications in software development.