Object-Oriented Programming (OOP) in .NET

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of fields (often known as attributes or properties) and code in the form of procedures (often known as methods).

In .NET, OOP principles are fundamental and are implemented using C# and Visual Basic. Understanding OOP is crucial for building robust, maintainable, and scalable applications.

Key OOP Concepts

The four main pillars of OOP are:

Classes and Objects

In .NET, a class is a blueprint for creating objects. It defines the properties (data members) and methods (member functions) that objects of that class will have. An object is an instance of a class.

Example: Defining a Class in C#


public class Dog
{
    // Properties
    public string Name { get; set; }
    public string Breed { get; set; }

    // Constructor
    public Dog(string name, string breed)
    {
        Name = name;
        Breed = breed;
    }

    // Method
    public void Bark()
    {
        Console.WriteLine($"{Name} says Woof!");
    }
}
            

Example: Creating and Using an Object in C#


// Create an instance of the Dog class
Dog myDog = new Dog("Buddy", "Golden Retriever");

// Access properties
Console.WriteLine($"My dog's name is {myDog.Name}.");
Console.WriteLine($"He is a {myDog.Breed}.");

// Call a method
myDog.Bark();
            

Encapsulation

Encapsulation is achieved through access modifiers (like public, private, protected) and properties. Properties provide a flexible mechanism to read, write, or compute the value of a private field.

Tip: Using properties instead of directly exposing fields helps maintain control over data access and allows for validation logic.

Inheritance

Inheritance allows a class to inherit members from another class. This is represented by the colon (:) symbol in C#.

Example: Inheritance in C#


public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Meow");
    }
}
            

Polymorphism

Polymorphism means "many forms". In OOP, it allows you to perform a single action in different ways. This is often achieved through virtual methods and method overriding.

Example: Polymorphism in C#


Animal myAnimal = new Animal();
Animal myCat = new Cat(); // Cat object is treated as an Animal

myAnimal.MakeSound(); // Output: Some generic animal sound
myCat.MakeSound();    // Output: Meow (due to overriding)
            

Abstraction

Abstraction is typically achieved using abstract classes and interfaces. An abstract class cannot be instantiated directly and may contain abstract methods (methods without an implementation) that must be implemented by derived classes. An interface defines a contract of methods, properties, and events that a class must implement.

Example: Abstract Class in C#


public abstract class Shape
{
    public abstract double Area(); // Abstract method
}

public class Circle : Shape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public override double Area()
    {
        return Math.PI * Radius * Radius;
    }
}
            

Further Reading