The Source Code Library
Polymorphism in C#.NET
, Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism.
In C#, every type is polymorphic. Types can be used as their own type or as a Object instance, because any type automatically treats Object as a base type.
Polymorphism means having more than one form.
are used to implement polymorphism. Polymorphism is classified into compile time polymorphism or early binding or static binding and Runtime polymorphism or late binding or dynamic binding.
Compile time Polymorphism or Early Binding
The polymorphism in which compiler identifies which polymorphic form it has to execute at compile time it self is called as compile time polymorphism or early binding.
Advantage of early binding is execution will be fast. Because every thing about the method is known to compiler during compilation it self and disadvantage is lack of flexibility.
Examples of early binding are overloaded methods, overloaded operators and overridden methods that are called directly by using derived objects.
Runtime Polymorphism or Late Binding
The polymorphism in which compiler identifies which polymorphic form to execute at runtime but not at compile time is called as runtime polymorphism or late binding.
Advantage of late binding is flexibility and disadvantage is execution will be slow as compiler has to get the information about the method to execute at runtime.
Example of late binding is overridden methods that are called using base class object.
RELATED C#.NET SNIPPETS
Multiple Inheritance Example c#
Abstract Classes and Abstract Methods in C#.NET
Virtual Methods in C#.NET with example
Method Overriding in C#.NET
Method Overloading in C#.NET
Sealed classes and Sealed methods in C#.NET
Constructor and Destructor invoking sequence with inheritance
Different Types of Inheritance
What is Inheritance in C#