Hi there, I hope you are fine, me too. This is 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 its 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 occurr 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAPP
{
public class genericTest
{
public void fun(string name, string surname)
{
Console.WriteLine("Name:"+ name + "Surname:"+ surname);
}
public void fun(string name)
{
Console.WriteLine("Name:" + name);
}
}
class Program
{
static void Main(string[] args)
{
genericTest obj = new genericTest();
obj.fun("sourav", "kayal");
obj.fun("sourav");
Console.ReadLine();
}
}
}
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAPP
{
public class genericTest
{
//taking 2 string as parameters
public void fun(string name, string surname)
{
Console.WriteLine(name + surname);
}
//Taking two type paramaters and both are string in this example
public void fun<T>(T name, T surname)
{
Console.WriteLine(name.ToString() + surname.ToString());
}
}
class Program
{
static void Main(string[] args)
{
genericTest obj = new genericTest();
obj.fun("sourav", "kayal");
obj.fun<string>("sourav", "kayal");
Console.ReadLine();
}
}
}
It will take Type (T) as 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 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 sample output of the code above.
FinallyI 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, nice way to ask a scenario based question. Happy learning.