Example 1: Implementation of C# method overloading (static polymorphism)
Program.cs
Console.WriteLine("Area of Rectangle " + Shape.Rectangle(5, 10));
Console.WriteLine("Area of Rectangle {0}", Shape.Rectangle(3.14, 10));

class Shape
{
    public static int Rectangle(int a, int b)
    {
        return a * b;
    }
    public static double Rectangle(double a, double b)
    {
        return a * b;
    }
}
Output
kodingwindow@kw:~/csharp$ dotnet run
Area of Rectangle 50
Area of Rectangle 31.400000000000002 
kodingwindow@kw:~/csharp$ 
Example 2: Implementation of C# method overloading
Program.cs
Shape s = new Shape();
double area = s.Square(10);
Console.WriteLine("Area of Square " + area);

area = s.Rectangle(3.14, 10);
Console.WriteLine("Area of Rectangle {0}", area);

class Shape
{
    public double Square(int a)
    {
        return a * a;
    }
    public double Rectangle(double a, double b)
    {
        return a * b;
    }
}
Output
kodingwindow@kw:~/csharp$ dotnet run
Area of Square 100
Area of Rectangle 31.400000000000002 
kodingwindow@kw:~/csharp$ 
Advertisement