Method overloading in C#

Many programming languages ​​support a technique called default / optional parameters. This allows the programmer to make one or several parameters optional by giving a default value. This is particularly practical when adding functionality to the existing code

For example, you might want to add functionality to an existing function, for which it is necessary to add one or more parameters, by doing this, you will break the existing code by calling this function, because now it is necessary to cross the required quantity parameters Will not be able. To work for this, you can define newly added parameters as optional, and give them a default value, which will work before the parameters are added.

The default standards were introduced in C # version 4.0, but until that, C # coders are using a different technique, which basically does the same, which is called method overloading. This allows the programmer to define several methods with the same name, as long as they take a different set of parameters when you use sections of the .NET framework, you will soon find that this method Overload is used in whole place. A good example of this is the substrings () method of the string class. This is with an additional surcharge, such as:


string Substring (int startIndex)
string Substring (int startIndex, int length) 

You can call it with one or two parameters if you say it with only one parameter, then the length parameter is considered the rest string, whenever we want to go to the last part of the string, it saves us time .

So, by defining several versions of the same function, how many places do we stop the same code? It's really very simple: we do all our complex work in simple versions of the law. Consider the following example:


class SillyMath
{
    public static int Plus(int number1, int number2)
    {
        return Plus(number1, number2, 0);
    }

    public static int Plus(int number1, int number2, int number3)
    {
        return number1 + number2 + number3;
    }
}

We define one plus method in two different versions. Take the first two parameters, add two numbers, while the second version has three numbers. Actual work is done in the version which takes three numbers - if we just want to add two, then we call the three parameter version, and only use 0 as the third parameter, which is the default value There is work in the form that I know, I know, it is a silly example, as shown in the name of class, but it should give you an idea how all this works.

Now, whenever you feel like doing advanced math by adding the total number of four digits (just joking around here), then adding a new surcharge is very easy:


class SillyMath
{
    public static int Plus(int number1, int number2)
    {
        return Plus(number1, number2, 0);
    }

    public static int Plus(int number1, int number2, int number3)
    {
        return Plus(number1, number2, number3, 0);
    }

    public static int Plus(int number1, int number2, int number3, int number4)
    {
        return number1 + number2 + number3 + number4;
    }
}

The good thing about this is that all your existing call-plus systems will continue to work, as if nothing has changed. The more you use C #, the more you will learn to appreciate the method overload.