C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The methods are stored at different locations within the metadata. We have a method group with a single name.
C# program that has overloaded methods
class Program
{
static void Main()
{
MethodA();
MethodA("");
}
static void MethodA()
{
}
static void MethodA(string a)
{
}
}
Example: 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.
Empty StringC# program that has no overloaded methods
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);
}
}
}
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.
ConsoleC# program that implements overloaded method
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);
}
}
Compiled method that does not use overloading: IL
.method private hidebysig static void ShowString(string 'value') cil managed
{
// Code size 31 (0x1f)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldsfld string [mscorlib]System.String::Empty
IL_0006: call bool [mscorlib]System.String::op_Equality(string,
string)
IL_000b: brfalse.s IL_0018
IL_000d: ldstr "Popular"
IL_0012: call void [mscorlib]System.Console::WriteLine(string)
IL_0017: ret
IL_0018: ldarg.0
IL_0019: call void [mscorlib]System.Console::WriteLine(string)
IL_001e: ret
}
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.
Compiled methods that use overloading: IL
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 22 (0x16)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: call void OverloadA::ShowString()
IL_000b: ldstr "Category"
IL_0010: call void OverloadA::ShowString(string)
IL_0015: ret
}
.method private hidebysig static void ShowString() cil managed
{
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "Popular"
IL_0005: call void OverloadA::ShowString(string)
IL_000a: ret
}
So: 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.
Null StringsC# program that causes ambiguous error
using System.Text;
class Program
{
static void Test(string value)
{
}
static void Test(StringBuilder value)
{
}
static void Main()
{
Test(null);
}
}
Output
Error CS0121
The call is ambiguous between the following methods or properties:
'Program.Test(string)' and 'Program.Test(StringBuilder)'