What is an Overloaded Method?


Sometimes when programming, you might want to create a number of methods which internally have similar logic but accept different parameters. For example, an addition method called AddNumbers which, erm… adds numbers together, might accept two parameters of type int. But what if you want to add long values, or double values? You could create three separate methods called:

  • AddInt(int num1, int num2)
  • AddLong(long num1, long num2)
  • AddDouble(double num1, double num2)

But why create three methods with different names, therefore having to remember all three, when you can create three methods with the same name but different parameters?

With method overloading in C# you could instead create:

  • Add(int num1, int num2)
  • Add(long num1, long num2)
  • Add(double num1, double num2)

Note the method name is the same but the parameters are different. But this is a silly example for real-world use. Why would you want to create a method to add numbers together when you can simply use the ‘+’ keyword?

So let’s create a more useful example. Let’s create a method which searches for a string within another string.

private bool IsWordInString(string wordToFind, string largeText)
    bool found = false;
    int position = largeText.IndexOf(wordToFind);
    if (position != -1)
        found = true;

    return found;

To call this method you would do the following:

string longText = "This is a bunch of words which have no meaning at all.";
bool found = IsWordInString("bunch", longText);

Now let’s overload the method to accept a StreamReader object instead of a string, therefore allowing the method to read from a file.

private bool IsWordInString(string wordToFind, StreamReader stream)
    string input = null;

    while ((input = stream.ReadLine()) != null)
        if (input.IndexOf(wordToFind, 0) > 0)
            return true;
    return false;

You would call the method like this:

StreamReader stream = File.OpenText("LargeText.txt");
bool found = IsWordInString("bunch", stream);

As you can see, both above methods have the same name but their parameters are different. Logically they both do a similar function – identify if the passed word exists in either the passed string or the file. This could have been just as easily implemented using methods with different names, but why do that when you can use a single name? Obviously, when using method overloading, you must only overload where it’s appropriate, i.e. where the underlying functionality performs a similar task.


DaveOnCSharp.com runs on the Thesis Theme

How smart is your Theme? How good is your support? Check out ThesisTheme for WordPress.

The Thesis Theme is one of the best Wordpress frameworks out there, especially if you understand software development, which since you're here you probably do. Thesis allows you to design post and page templates using a drag-and-drop system which gives you access to all the relevant page elements you would normally have to manually access through code. This is why understanding development techniques definitely helps, even though you don't need to write any code to get Thesis up and running.

So go check out the incredible features at DIYThemes and start using Thesis now!

1 comment… add one
  • jo

    Thanks for web tutorials! Topics such as this one will really help me a lot.
    Keep on posting and God Bless! 🙂

Leave a Comment