Why We use Polymorphism in C#


Polymorphism means ‘one name, many forms‘. Essentially, polymorphism is the capability of one object to behave in multiple ways. In C#, Polymorphism can be achieved in two ways:

    1. Operation Polymorphism
    2. Inclusion Polymorphism

Operation Polymorphism:

Operation polymorphism is implemented using overload methods and operators. The overload methods are ‘selected’ for invoking by matching arguments, in terms of number, type and order. This information is known to the compiler at the time of compilation and therefore, the compiler can select and bind the appropriate method to the object for a particular call at compile time itself. This process is called early binding or static binding or static linking. Early binding simply means that an object is bound to its method call at compile time. So, It is also known as Compile-time polymorphism.

Inclusion Polymorphism:

Inclusion polymorphism is achieved through the use of virtual functions. Assume that class A implements a virtual method M and classes B and C that are derived from A override the virtual method M. When B is cast to A, a call to the method M from A is dispatched to B. Similarly, When C is cast to A, a call to M is dispatched to C. The decision on exactly which method to call is delayed until runtime. So, it is also known as Runtime Polymorphism. Since the method is linked with a particular class much later after compilation, this process is termed late binding. It is also known as Dynamic Binding because the selection of the appropriate method is done dynamically at runtime.