C#

Introduction to Object-Oriented Programming in C#


Object-Oriented Programming (OOP) is a fundamental paradigm in C# and many other programming languages. In this introductory guide, you'll learn the core concepts of OOP and how to implement them in C#.

What is Object-Oriented Programming?

Object-Oriented Programming is a programming paradigm that organizes code into reusable, self-contained objects. These objects represent real-world entities, and they interact with each other through well-defined interfaces. OOP focuses on four main principles:

  • Encapsulation: Bundling data (attributes) and methods (functions) that operate on that data into a single unit called a class.
  • Inheritance: Creating new classes (subclasses or derived classes) based on existing classes (base classes or superclasses) to inherit their properties and behaviors.
  • Polymorphism: The ability of different classes to be treated as instances of a common base class, enabling objects of different types to be used interchangeably.
  • Abstraction: Simplifying complex reality by modeling classes based on real-world entities, hiding unnecessary details while exposing the essential features.

Classes and Objects

In C#, classes are used to define the blueprint for creating objects. Objects are instances of classes, and they contain both data (attributes) and methods (functions). Here's how you create a simple class and use it to create an object:

class Car
{
    public string Make;
    public string Model;
    public void Start()
    {
        Console.WriteLine(`Car is starting.`);
    }
}
// Creating an object of the Car class
Car myCar = new Car();
myCar.Make = `Toyota`;
myCar.Model = `Camry`;
myCar.Start();
    

Inheritance

Inheritance allows you to create new classes based on existing classes. The new class inherits the attributes and methods of the base class. For example:

class Vehicle
{
    public string Make;
    public string Model;
    public void Start()
    {
        Console.WriteLine(`Vehicle is starting.`);
    }
}
class Car : Vehicle
{
    public void Accelerate()
    {
        Console.WriteLine(`Car is accelerating.`);
    }
}
Car myCar = new Car();
myCar.Make = `Honda`;
myCar.Model = `Civic`;
myCar.Start();
myCar.Accelerate();
    

Polymorphism

Polymorphism allows objects of different classes to be treated as instances of a common base class. This enables you to work with a variety of objects interchangeably. For example:

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine(`Drawing a shape.`);
    }
}
class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine(`Drawing a circle.`);
    }
}
class Square : Shape
{
    public override void Draw()
    {
        Console.WriteLine(`Drawing a square.`);
    }
}
void DrawShape(Shape shape)
{
    shape.Draw();
}
Shape myShape = new Circle();
DrawShape(myShape);
myShape = new Square();
DrawShape(myShape);
    

Abstraction

Abstraction allows you to focus on the essential features of a class while hiding unnecessary details. You can create abstract classes and methods that serve as templates for derived classes. For example:

abstract class Animal
{
    public string Name;
    public abstract void MakeSound();
}
class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine(`Dog barks.`);
    }
}
class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine(`Cat meows.`);
    }
}
Animal myDog = new Dog();
myDog.Name = `Fido`;
myDog.MakeSound();
Animal myCat = new Cat();
myCat.Name = `Whiskers`;
myCat.MakeSound();
    

Conclusion

Object-Oriented Programming is a powerful paradigm that promotes code reusability, modularity, and maintainability. You've learned the core principles of OOP, how to create classes and objects, implement inheritance, utilize polymorphism, and apply abstraction in C#.

Practice these concepts in your C# programs to build efficient, well-structured applications. As you advance in your programming journey, you'll explore more advanced OOP features and design patterns to enhance your coding skills.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.