C# Polymorphism Tutorial For Beginners With Examples

Polymorphism means an object which has different forms.
Polymorphism is exhibited when different objects expose the same type of methods and properties but with different behavior.

Implementing Polymorphism

Below ways can be used to achieve polymorphism in C#:

Polymorphism through Interfaces

Polymorphism can be achieved using interfaces. Different classes using the same interface will expose the same methods and properties. These methods from different classes may behave differently.
Lets create an interface ICalculate having a Calculate method.
    public interface ICalculate
    {
        int Calculate(int x, int y);
    }
Now create two different classes Math and Account which implements the ICalculate interface.
        
    public class Math : ICalculate
    {
        int result;

        public int Calculate(int x, int y)
        {
            result = x + y;
            return result;
        }
    }

    public class Account : ICalculate
    {
        int result;

        public int Calculate(int x, int y)
        {
            result = x * y;
            return result;
        }
    }
        
Lets create another method which accepts any object which implements an ICalculate interface. This method should be a static method as it will be called from static Main method.
            
    static void ShowResult(ICalculate obj)
    {
        int x = 2, y = 3;

        Console.WriteLine(obj.Calculate(x, y));
    }
            
The above method can be called from the Main method as below:
    static void Main(string[] args)
    {
        Math mathObject = new Math();

        ShowResult(mathObject);

        Account accountObject = new Account();

        ShowResult(accountObject);

        Console.ReadLine();
    }
            
Output:
5
6
Here the ShowResult method gives different output based on the object passed to it. Objects of type Account and Math exhibit polymorphism as both expose the same method but with different behavior.

Polymorphism through Inheritance

To understand polymorphism through inheritance lets create a base class and some derived classes.
Below the Calculator class is the base class. It has a virtual method Calculate that can be overridden by the derived classes.
    public class Calculator
    {
        int result;

        public virtual int Calculate(int x, int y)
        {
            result = x - y;
            return result;
        }
    }
        
Lets create two more classes which will derive from Calculator class. These derived classes will override the Calculate method of the base class.
   public class Math : Calculator
    {
        int result;

        //Override the base class method
        public override int Calculate(int x, int y)
        {
            result = x + y;
            return result;
        }
    }

    public class Account : Calculator
    {
        int result;

        //Override the base class method
        public override int Calculate(int x, int y)
        {
            result = x * y;
            return result;
        }
    }        
        
To prove polymorphism a method named ShowResult is created which accepts objects of base class type only.
        
    static void ShowResult(Calculator obj)
    {
        int x = 2, y = 3;

        Console.WriteLine(obj.Calculate(x, y));
    }        
        
Now lets write a main method which will call the ShowResult method.
        
    static void Main(string[] args)
    {
        Calculator mathObject = new Math();

        ShowResult(mathObject);

        Calculator accountObject = new Account();

        ShowResult(accountObject);

        Console.ReadLine();
    }
        
Output:
5
6
In the above code the ShowResult method is called two times and an object of type Calculatorwas passed to it. Both the times the Calculator object passed to the ShowResult method was initialized to two different derived types.
Since both the derived types had a Calculate method which behaved differently so polymorphism is exhibited here.
Polymorphism is one of the fundamentals of Object Oriented Programming (OOP) along with EncapsulationAbstraction and Inheritance.