Introduction to OOP in C#

Tutorial 1 of 5

Introduction

In this tutorial, we are going to get familiar with Object-Oriented Programming (OOP) in C#. OOP is a programming paradigm that revolves around the concept of objects, which can contain data and code.

By the end of this tutorial, you will have a basic understanding of:

  • The four principles of OOP: Encapsulation, Inheritance, Polymorphism, and Abstraction.
  • How to implement these principles in C#.

Prerequisites:

  • Basic understanding of C# syntax
  • An installed version of .NET SDK
  • A text editor or IDE such as Visual Studio

Step-by-Step Guide

Encapsulation

Encapsulation is the practice of keeping fields within a class private, then providing access to them via public methods. It's a protective barrier that keeps the data and code safe from outside interference and misuse.

Example:

public class Employee
{
    private string name;

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

In this example, the name field is private, so it can only be accessed within the Employee class. We provide a way to get and set the name with the Name property.

Inheritance

Inheritance is a process in which one class acquires the properties and functionalities of another class. The class whose properties and functionalities are inherited is known as the base class, and the class that inherits those properties and functionalities are known as the derived class.

Example:

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

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

In this example, the Dog class inherits from the Animal class.

Polymorphism

Polymorphism allows actions to act differently based on the object that they are acting upon. In C#, polymorphism is implemented using many techniques such as method overriding and interfaces.

Example:

public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

public class Pig : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The pig says: wee wee");
    }
}

In this example, Animal has a method MakeSound, and the Pig class overrides this method.

Abstraction

Abstraction means using simple things to represent complexity. In C#, we use abstract classes and interfaces to achieve abstraction.

Example:

public abstract class Animal
{
    public abstract void animalSound();
}

public class Pig : Animal
{
    public override void animalSound()
    {
        // The body of animalSound() is provided here
        Console.WriteLine("The pig says: wee wee");
    }
}

In this example, Animal is an abstract class, and animalSound is an abstract method. The Pig class provides the implementation for animalSound method.

Code Examples

Let's see some practical examples:

  1. Encapsulation
public class Bank
{
    private double balance;

    public double Balance
    {
        get { return balance; }
        private set { balance = value; }
    }

    public void Deposit(double amount)
    {
        if(amount > 0)
            Balance += amount;
    }

    public void Withdraw(double amount)
    {
        if(amount > 0 && Balance >= amount)
            Balance -= amount;
    }
}

In this example, the balance field is private. We can manipulate it through Deposit and Withdraw methods, but we can't directly change its value from outside the class.

  1. Inheritance
public class Vehicle
{
    public string Brand { get; set; }
}

public class Car : Vehicle
{
    public int Doors { get; set; }
}

In this example, Car class inherits from Vehicle class. So, Car has both Brand and Doors properties.

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

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

In this example, the Shape class has a Draw method, and the Circle class overrides this method to draw a circle.

  1. Abstraction
public abstract class Shape
{
    public abstract void Draw();
}

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

In this example, Shape is an abstract class with an abstract method Draw. The Circle class provides the implementation for Draw method.

Summary

In this tutorial, you've learned about the four principles of OOP in C#: Encapsulation, Inheritance, Polymorphism, and Abstraction. Understanding these principles will help you design your code in a more organized and efficient way.

For further learning, you can look into more advanced topics such as Interfaces, Generics, and Delegates in C#.

Practice Exercises

  1. Create a Person class with properties Name and Age, and a method Greet which outputs "Hello, my name is {Name} and I am {Age} years old".

  2. Create a Student class that inherits from the Person class, with an additional property Grade. Override the Greet method to also include the grade of the student.

  3. Create a Shape abstract class with an abstract method CalculateArea. Then, create Square and Circle classes that inherit from Shape and implement the CalculateArea method.

Remember to test your classes by creating an instance of each and invoking their methods. Happy coding!