Function Overloading in 2 Ways

Hi there, I hope you are fine, me too. These are some very fundamental concepts of OOP and we implement function overloading every now and then in day to day work. Let me tell the story behind the article. One of my colleagues went for an interview as a .NET developer and in the interview, he was asked to implement function overloading where the number of variables and their type will be the same for all functions. So, after knowing the answer and the trick, I thought it will be nice if I share it with the community. Actually, many of you understand the concept but the implementation and scenario may not occur to you during the interview.
 
Fine, so let's learn the simple traditional way to implement function overloading. We will just keep the function name the same and we will change the number of arguments. Very simple. Here is the code implementation.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.    
  7. namespace ConsoleAPP  
  8. {  
  9.     
  10.     public class genericTest  
  11.     {  
  12.         public void fun(string name, string surname)  
  13.         {  
  14.             Console.WriteLine("Name:"+ name + "Surname:"+ surname);  
  15.         }  
  16.         public void fun(string name)  
  17.         {  
  18.             Console.WriteLine("Name:" + name);  
  19.         }  
  20.           
  21.     }  
  22.     class Program  
  23.     {  
  24.         static void Main(string[] args)  
  25.         {  
  26.             genericTest obj = new genericTest();  
  27.             obj.fun("sourav""kayal");  
  28.             obj.fun("sourav");  
  29.             Console.ReadLine();  
  30.         }  
  31.     }  

And the expected output is here.
 
 
Now, let's try to implement the same concept of function overloading using a generic function. I hope many of you understand the concept of Generic in C#. If not, I suggest you go through the concept at first. Generic is a way to represent something in a general way and we can implement a generic implementation of many components like class, function, list, and so on. In this example, we will implement a generic function. The first function will take two strings as parameters and the second function is a bit interesting.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.    
  7. namespace ConsoleAPP  
  8. {  
  9.     public class genericTest  
  10.     {  
  11.         //taking 2 string as parameters  
  12.         public void fun(string name, string surname)  
  13.         {  
  14.             Console.WriteLine(name + surname);  
  15.         }  
  16.         //Taking two type parameters and both are string in this example  
  17.         public void fun<T>(T name, T surname)  
  18.         {  
  19.             Console.WriteLine(name.ToString() + surname.ToString());  
  20.         }  
  21.     }  
  22.     class Program  
  23.     {  
  24.         static void Main(string[] args)  
  25.         {  
  26.             genericTest obj = new genericTest();  
  27.             obj.fun("sourav""kayal");  
  28.             obj.fun<string>("sourav""kayal");  
  29.             Console.ReadLine();  
  30.         }  
  31.     }  

It will take Type (T) as a parameter. So, at runtime we can define the type value of T. Now, at runtime, if we define “string” as a type value of both parameters then the function will behave exactly like the first function, and please note that both functions are enjoying the same name and the same parameter if we specify a string as the type of the second function.
 
And this is the trick to implement function overloading where the number of arguments and the type of the argument and function name and everything is the same. Here is a sample output of the code above.
 
 
Finally
 
I hope you have enjoyed the implementation and concepts. The truth to be told, when I heard the question the first time, the trick did not occur to me but after knowing the answer I thought Oh, a nice way to ask a scenario-based question. Happy learning.


Recommended Free Ebook
Similar Articles