First example. Overloaded methods are separate in the compiled program. A method receiving a string parameter is separate from one receiving no parameter or an int.
Note The methods are stored at different locations within the metadata. We have a method group with a single name.
class Program
{
static void Main()
{
MethodA();
MethodA("");
}
static void MethodA()
{
}
static void MethodA(string a)
{
}
}
Example 2. Next we look at one situation where you can apply overloaded methods. This is a simple way of improving code readability and performance. Here's some code that is problematic.
Detail The code writes the word Popular to the screen if the string argument is empty.
And Otherwise, it writes the category name. But this code is inefficient and confusing. We can entirely avoid the string.Empty argument.
using System;
class Program
{
public static void Main()
{
ShowString(string.Empty);
ShowString("Category");
}
static void ShowString(string value)
{
if (value == string.Empty)
{
Console.WriteLine("Popular");
}
else
{
Console.WriteLine(value);
}
}
}
Example 3. The code above has an unnecessary branch, and this is not optimized out. It calls into ShowString, but we know that the string is never empty at that call site.
Here There is an overload with no parameters, and one with a string parameter. Overloaded methods always have different parameters.
Note Look at what happens when ShowString is called the second time. It goes directly from the call site to the Console.WriteLine part.
using System;
class Program
{
public static void Main()
{
ShowString();
ShowString("Category");
}
static void ShowString()
{
// Send default argument to overload.
ShowString("Popular");
}
static void ShowString(string value)
{
// We don't need an if check here, which makes// ... calling this method directly faster.
Console.WriteLine(value);
}
}
Internals. Here we look into the intermediate language to see what the overloaded methods become when first compiled. Let's look inside the ShowString method that has the if-statement.
Internals, continued. The if chain in ShowString uses an op_Equality. None of this is compiled out and it also isn't optimized away during JIT compilation.
Next We show how the overloaded methods are called in the second example. The methods are separate.
And The compiler can easily tell the difference between overloads with different parameters.
Error, ambiguous call. Suppose we have 2 methods that both receive a reference type that can be null. If we pass null, the compiler cannot decide which method to invoke.
Detail The best solution here is to not pass null to methods that are overloaded like this—redesign the program to make more sense.
Tip We could rename methods, or pass an empty string instead of a null string. The best solution may depend on your project.
using System.Text;
class Program
{
static void Test(string value)
{
}
static void Test(StringBuilder value)
{
}
static void Main()
{
Test(null);
}
}Error CS0121
The call is ambiguous between the following methods or properties:
'Program.Test(string)' and 'Program.Test(StringBuilder)'
A summary. It is possible to refactor code to use method overloading. The overloads are easily inferred by the compiler. You can streamline code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.