Overloaded methods improve code clarity. When designing complex programs, code changes and some branches become unnecessary.
We add overloaded methods to eliminate complexity and enhance performance. Overloads can differ based on argument types.
Overloaded methods are separate in the compiled program. A method receiving a string
parameter is separate from one receiving no parameter or an int
.
class Program { static void Main() { MethodA(); MethodA(""); } static void MethodA() { } static void MethodA(string a) { } }
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.
string
argument is empty.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); } } }
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.
string
parameter. Overloaded methods always have different parameters.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); } }
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.
.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 }
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.
.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 }
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.
null
to methods that are overloaded like this—redesign the program to make more sense.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)'
It is possible to refactor code to use method overloading. The overloads are easily inferred by the compiler. You can streamline code.