| 0 | kirasiris | Categories: Tags: , , , , ,

overloading c# csharp

Method overloading is a feature that is mostly found in OOP languages. These methods can share the same name as their neighbor method but with the difference of getting different data types as their parameters. Once the program is compiled, the correct method will be chosen according to how it was called from the main app.

Some refer to methods as functions.

Kevin Uriel Azuara Fonseca

Playing with some Methods

We could have a class and some methods to calculate the area of X shape but written in different ways. Let’s say, instead of using getArea(int a), we could easily use getArea(double a) or getArea(string a) also.

Math () {
  getArea(){}
  public int getArea(int a){
    return a*a;
  }
  public double getArea(double a){
    return a*a;
  }
  public string getArea(string a){
    return a*a;
  }
}

As mentioned before, the requirements for method overloading would be to have methods sharing the exact same name; the only functionality that should differ from each other is their data types, number and order of parameters. This can make it more easy for the compiler to run the proper one.

Constructors are overloaded by default when an object with no values is created. An example of this concept would look similar to this:

Car () {
  // An object with an empty brand='' and model='';
  Car(){
    Console.WriteLine("To run if not parameters are passed");
  }
  // An object with values passed brand='this' and model='that';
  Car(brand, model){
    Console.WriteLine("This is the brand (0) and this is the model (1)");
  }
  // An object to instantiate brand='this'
  Car(brand){
    Console.WriteLine("This is the brand (0)");
  }
}

As per my knowledge, Method Overloading is often used to make several methods logically look as a single method. As previously shown in the getArea() example, there are four physical methods but – only one – is empty so it can be expanded to work on all kind of shapes.

With this, I hope to have given you a somewhat easy explanation of how overloading works. There’s something called Polymorphism or Method Overriding also but that’s for a different topic that I will do my best to cover in one of the next posts.

As a goodbye note, I leave you with this second example of overloading with C#.

using  static System.Console;

namespace FourthConsoleApplicationUnitMethods
{
    class UnitMethods {

        // Method Overloading
        static int Add(int a, int b) {
            int result = a + b;
            return result;
        }

        static double Add(double a, double b) {
            double result = a + b;
            return result;
        }

        static void Main(string[] args) {

            int resultInt = Add(5,10);
            double resultDouble = Add(10.00, 20.00);

            WriteLine("The integer addition is: " + resultInt);
            WriteLine("The double addition is: " + resultDouble);

            ReadLine();

        }
    }
}

If you think that having written this article was a good idea, then you will probably like to read my previous post on JavaScript, click here.

Bye-Bye 🙂

Leave a Reply

Back to Top