3.3 Method-Overloading

Objectives

  1. Learn what overloading is

  2. Learn why we need overloading

  3. Write some overloaded methods

Discussion

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

File Location

  1. Right click on your solution

  2. Go to Add > New Project and select Console App (.NET Framework)

  3. Name it MethodOverloading

Example

An example would be a program that has different attack parameters. With the example below, the player can do an attack with no parameters, one parameter for attackPoints, or two parameters of attackPoints and weaponName. The compiler will choose which one to use based on what fits. Add the code in your Program.cs file:

class Player
    {
//      1
        public void Attack()
        {
            Console.WriteLine("The player attacked for 5 points");
        }
//      2
        public void Attack(int attackPoints)
        {
            Console.WriteLine("The player attacked for {0} points and has the sword", attackPoints);
        }
//      3
        public void Attack(int attackPoints, string weaponName)
        {
            Console.WriteLine("The player attacked for {0} points and has a {1}", attackPoints, weaponName);
        }
    }
  1. Base Method

  2. Overloaded method with 1 parameter

  3. Overloaded method with 2 parameters

One More Example

class Program
    {
        static void Main(string[] args)
        {
            Music ledZeppelin = new Music();
            ledZeppelin.AnnounceGreatMusic(); 
            ledZeppelin.AnnounceGreatMusic("Carr O'Connor");
            ledZeppelin.AnnounceGreatMusic("Led Zeppelin", "Physical Graffiti");
            ledZeppelin.AnnounceGreatMusic("Metallica", "Rage Against the Machine(I know they're not really metal)", "Black Sabbath", "Disturbed", "Gucci Mane");

            Console.ReadLine();
        }
    }

public class Music
    {
//      1         1*
        public virtual void AnnounceGreatMusic() 
        {
            Console.WriteLine("It's impossible to argue that the greatest music of all time comes from Michael Bolton.");
        }
//      2
        public void AnnounceGreatMusic(string name) 
        {
            Console.WriteLine("Anything by {0} is the greatest music of all time", name);
        }
//      3
        public void AnnounceGreatMusic(string name, string album) 
        {
            Console.WriteLine("{0} is the best in the world. Check out {1}. It's a great album.", name, album);
        }
//      4       
        public virtual void AnnounceGreatMusic(string band1, string band2, string band3, string band4, string band5)
        {
            Console.WriteLine("The five greatest metal bands are {0} {1} {2} {3} {4}", band1, band2, band3, band4, band5);
        }
    }
  1. Base Method

  2. Overloaded method with one parameter

  3. Overloaded method with two parameters

  4. Overloaded method with five parameters

Discussion

In this example we have 4 different AnnounceGreatMusic() methods and we used overloading to make this happen. You can see we have different parameters for each of the AnnounceGreatMusic() methods. When the program runs, it will check to see what parameters it has and check to see if any of your AnnounceGreatMusic() methods match those parameters. In our Main() method above, we assigned some different parameters to the methods to test it out.

Challenge

Make a few different addition methods within a math class and use overloading to take in different parameters.

Next: Constructors

Last updated