Search This Blog

Friday, 20 July 2012

Method overloading


It allows the programmer do define several methods with the same name, as long as they take a different set of parameters.

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

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;
    }
}

Example 2:

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;
    }
}


PREVIOUS CHAPTER
NEXT CHAPTER

No comments:

Post a Comment