TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Overload Method

Use overloaded method calls. Inspect the IL code for overloads.
Overload. Overloaded methods improve code clarity. When designing complex programs, code changes and some branches become unnecessary.
Overload, notes. We add overloaded methods to eliminate complexity and enhance performance. Overloads can differ based on argument types.Type
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.

C# program that has overloaded methods 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.

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 String
C# 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); } } }
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.

Console
C# 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); } }
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.IL
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 }
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.

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 }
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.

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 Strings
C# 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)'
A summary. It is possible to refactor code to use method overloading. The overloads are easily inferred by the compiler. You can streamline code.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf