Method Overloading
Polymorphism is one of the main characteristics of Object Oriented Programming (OOP). Put simply, it allows an object to behave in various ways depending on the manner in which it is used. For example, you could have a method execute differently based on the type and/or number of parameters passed to it. Method Overloading is what makes this example possible. To accomplish Method Overloading, a developer can define two or more methods with the same name. Each method will take a different set of parameters. The parameter combination or signature, is what the compiler uses to determine which method to use.

Good examples of Method Overloading in the .NET Framework include (but are not limited to) the Console.WriteLine() and Substring() methods. You can pass different types to the Console.WriteLine() method and it will work because there are variations of the methods that will accept the different types.

–
C# Program
using System; namespace MethodOverloading { class Program { static void Main(string[] args) { MethodOverloadPlay(10, 3); Console.WriteLine("+++++"); MethodOverloadPlay(5, 5, 6); Console.WriteLine("+++++"); MethodOverloadPlay("SP"); Console.ReadLine(); } static void MethodOverloadPlay(int number1, int number2) { int result = number1 + number2; Console.WriteLine(result); } static void MethodOverloadPlay(int number1, int number2, int number3) { int result = number1 + number2 + number3; Console.WriteLine(result); } static void MethodOverloadPlay(string string1) { Console.WriteLine("Breaking Benjamin & Sick Puppies Rock!"); } } }
–
Result

–
The main advantage of Method Overloading is code readability. Programming languages that do not allow Method Overloading require the developer to create totally separate methods for each variation of the input. Blackwasp Consulting states the following example: in the ANSI C programming language to truncate a value you would use trunc, truncf or truncl according to the data type being rounded. In C#, method overloading allows you to always call Math.Truncate.
–
Additional Information
- C# Method Overloading – BlackWasp Consulting
- Guidelines for Method Overloading – Dave Donaldson
- Method overloading – CSharp .Net-tutorials
- Method overloading in C# .Net – Coder Source
–
^..^
Posted on November 27, 2009, in .NET General, C#, Development. Bookmark the permalink. 3 Comments.







Unfortunately method overloading has nothing to do with polymorphism.
An interview question that we often pose candidates is “what is the difference between method overloading and method overriding?”
Method overloading has to do with differentiating methods by not only their name but also the parameters they receive. Method overriding has to do with how a the method to call is determined at runtime. Method overloading, on the other hand, is determined at compile time. Very different things.
Eric,
Thank you for the information. So Method Overriding is related to Polymorphism, but Method Overloading is not?
I come from a Visual FoxPro background, but have been trying to pick up C# for some time.
Again, thank you for your comment.
Pingback: Dew Drop – November 28, 2009 | Alvin Ashcraft's Morning Dew