C# Polymorphism with Examples

In c#, Polymorphism means providing an ability to take more than one form, and it’s one of the main pillar concepts of object-oriented programming after encapsulation and inheritance.

 

Generally, polymorphism is a combination of two words, poly, and another one is morphs. Here poly means “multiple” and morphs means “forms” so polymorphism means many forms.

 

In c#, polymorphism provides an ability for the classes to implement different methods called through the same name. It also provides an ability to invoke a derived class's methods through base class reference during runtime based on our requirements.

 

In c#, we have two different kinds of polymorphisms available, which are

 

  • Compile Time Polymorphism
  • Run Time Polymorphism

C# Compile Time Polymorphism

In c#, Compile Time Polymorphism means defining multiple methods with the same name but with different parameters. Using compile-time polymorphism, we can perform different tasks with the same method name by passing different parameters.

 

In c#, the compile-time polymorphism can be achieved by using method overloading, and it is also called early binding or static binding.

 

Following is the code snippet of implementing a method overloading to achieve compile-time polymorphism in c#.

 

public class Calculate

{
    public void AddNumbers(int a, int b)
    {
        Console.WriteLine("a + b = {0}", a + b);
    }
    public void AddNumbers(int a, int b, int c)
    {
        Console.WriteLine("a + b + c = {0}", a + b + c);
    }
}

If you observe the above “Calculate” class, we defined two methods with the same name (AddNumbers), but with different input parameters to achieve method overloading, this is called a compile time polymorphism in c#.

C# Compile Time Polymorphism Example

Following is the example of implementing a compile-time polymorphism in the c# programming language.

 

using System;

namespace Tutlane
{
    public class Calculate
    {
       public void AddNumbers(int a, int b)
       {
          Console.WriteLine("a + b = {0}", a + b);
       }
       public void AddNumbers(int a, int b, int c)
       {
          Console.WriteLine("a + b + c = {0}", a + b + c);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          Calculate c = new Calculate();
          c.AddNumbers(1, 2);
          c.AddNumbers(1, 2, 3);
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

When we execute the above c# program, we will get the result below.

 

C# Compile Time Polymorphism Example Result

 

This is how you can achieve compile-time polymorphism in c# by implementing a method overloading based on our requirements.

C# Run Time Polymorphism

In c#, Run Time Polymorphism means overriding a base class method in the derived class by creating a similar function. This can be achieved by using override & virtual keywords and the inheritance principle.

 

Using run-time polymorphism, we can override a base class method in the derived class by creating a method with the same name and parameters to perform a different task.

 

In c#, the run time polymorphism can be achieved by using method overriding, which is also called late binding or dynamic binding.

 

Following is the code snippet of implementing a method overriding to achieve run time polymorphism in c#.

 

// Base Class
public class Users
{
    public virtual void GetInfo()
    {
       Console.WriteLine("Base Class");
    }
}
// Derived Class
public class Details: Users
{
    public override void GetInfo()
    {
       Console.WriteLine("Derived Class");
    }
}

If you observe the above code snippet, we created two classes (“Users”, “Details”), and the derived class (Details) is inheriting the properties from the base class (Users). We are overriding the base class method GetInfo in the derived class by creating the same function to achieve method overriding; this is called a run time polymorphism in c#.

 

Here, we defined a GetInfo method with a virtual keyword in the base class to allow the derived class to override that method using the override keyword.

 

Only the methods with a virtual keyword in the base class are allowed to override in the derived class using the override keyword. To know more about it, check this Method Overriding in C#.

C# Run Time Polymorphism Example

Following is the example of implementing a run time polymorphism in the c# programming language.

 

using System;

namespace Tutlane
{
    // Base Class
    public class BClass
    {
       public virtual void GetInfo()
       {
          Console.WriteLine("Learn C# Tutorial");
       }
    }
    // Derived Class
    public class DClass : BClass
    {
       public override void GetInfo()
       {
          Console.WriteLine("Welcome to Tutlane");
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          DClass d = new DClass();
          d.GetInfo();
          BClass b = new BClass();
          b.GetInfo();
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

When you execute the above c# program, you will get the result below.

 

C# Runtime Polymorphism Example Result

 

This is how you can achieve run time polymorphism in c# by implementing a method overriding based on our requirements.