C# Polymorphism with Examples

The word polymorphism means that it has many forms. Basically, we can define polymorphism as the ability to represent a message in more than one way. A real example of polymorphism, a person can have several properties at the same time. As a man, he is at the same time a father, husband, employee. So the same person behaves differently in different situations. This is called polymorphism. one of the most important topics in the object-oriented programming language is polymorphism.

Types of Polymorphism

  • Compile-time Polymorphism
  • Runtime Polymorphism

Compile-time Polymorphism

In Compile time Polymorphism, In this type, we achieved polymorphism through operator overloading or function overloading. Functions are overloaded when they have the same name but many functions with different parameters. Changing the number of arguments and/or the type of argument can overload the task

Example of Function overload:

class OperatorOverloading
{
	public void method(int x)
	{
		Console.WriteLine(x);
	}
    public void method(double x)
    {
        Console.WriteLine(x);
    }
    public void method(int x, int y)
    {
        Console.WriteLine(x +y);
    }
}
public class Entry
{
	static public void Main()
	{
		operatoroverloading operatoroverloading = new operatoroverloading();
		operatoroverloading.method(10);
		operatoroverloading.method(10.20);
		operatoroverloading.method(10, 20);
	}
}

Output:

10
10.2
30

In the above example, a function called method works differently in three different situations, which are proprietary to the polymorphism.

C# also offers the ability to override operators. For example, you can create a string section operator ("+") to combine two strings. We know it's an additional operator. Thus, one “+” operator sits between entire openings, adds, and connects them when placed between array operations.

Runtime Polymorphism: 

The type of polymorphism that can be achieved by function overriding. On the other hand, function overriding occurs when a derivative class defines one of the functions of a member of the base class. This basic function is said to have been abolished

Example:

class overriding
    {
        public virtual void method()
        {
            Console.WriteLine("base");
        }
    }
    class newclass: overriding
    {
        public  override void method()
        {
            Console.WriteLine("derived");
        }
    }
    public class Entry
    {
        static public void Main()
        {
            overriding overriding = new newclass();
            overriding.method();
           
        }
    }

Output:

derived

Example:

using System;
class Program
{
    public void Add()
    {
        int first = 10;
        int second = 20;
        int result = first + second;
        Console.WriteLine(result);
    }
    public void Add(int first, int second)
    {
        int result = first + second;
        Console.WriteLine(result);
    }
    public void Add(string first, string second)
    {
        Console.WriteLine("First Name: " + first + " Second Name: " + second);
    }
    static void Main(string[] args)
    {
        Program program = new Program();
        program.Add();
        program.Add(15, 15);
        program.Add("Shahzad", "Hussain");
    }
}

Output:

30
30
First Name: Shahzad Second Name: Hussain