C# Polymorphism with Examples
Polymorphism mean many forms that we usually archived with static binding and dynamic binding.. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.
The below are two types of Polymorphism in C#:
1. Compile-Time Polymorphism (Static Binding):
When compiler decide which method or operator to call based on the number and types of arguments or operands, It is called compile time polymorphism. It usually occurs in from of method overloading or operator overloading.
1. 1 Method Overloading:
When you define multiple methods in a class with the same name but with different parameter lists. This method parameter must differ in the numbers, types, and order of parameters. It is called method overloading. Method overloading is a form of polymorphism.
public class FruitUtility
{
// Method with no parameters
public int AddFruitPrice()
{
return 0;
}
// Method with two integer parameters
public int AddFruitPrice(int price1, int price2)
{
return price1 + price2;
}
// Method with three double parameters
public double AddFruitPrice(double price1, double price2, double price3)
{
return…