Medium csharp

OOP — Method overloading #57

Problem

Overload Add for int and double.

Hints
  • Same method name, different parameters

Your practice code

Ready — edit the code above and click Run.

Solution

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(Calculator.Add(2, 3));
        Console.WriteLine(Calculator.Add(2.5, 3.1));

static class Calculator {
    public static int Add(int a, int b) => a + b;
    public static double Add(double a, double b) => a + b;
}
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Compile-time polymorphism via method overloading.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details