What Is Method Overloading In C#

In this Article, I will discuss Method Overloading in C#,
 
What is Method Overloading??
 
In some programming languages, we cannot create two methods with the same name inside a class. But as C# is an Object Oriented language, we can have multiple methods with the same name and this is called method overloading. In simple terms, Method Overloading is defined when multiple(more than one) methods with the same name are present in a class but with different parameters. We can have any number of method overloads.
 
Let's understand the example shown below:
 
Here in this example, Class named "Program" contains methods with the same name "Print_Output()". Method overloading works when the number of parameters is different or the order of parameters is different; with or without the change in return type. Methods with the same name are declared with no parameter, one parameter, and more than one parameter as required.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         // Method with no parameter  
  8.         public void Print_Output()  
  9.         {  
  10.             Console.WriteLine("Method 'Print_Output' with no parameter");  
  11.         }  
  12.   
  13.         // Method with one parameter of int type  
  14.         public void Print_Output(int val)  
  15.         {  
  16.             Console.WriteLine("Method 'Print_Output' with one int type parameter whose value is: " + val);  
  17.   
  18.         }  
  19.   
  20.         // Method with one parameter of string type  
  21.         public void Print_Output(string val)  
  22.         {  
  23.             Console.WriteLine("Method 'Print_Output' with one string type parameter whose value is: " + val);  
  24.         }  
  25.   
  26.         // Method with one parameter of double type  
  27.         public void Print_Output(double val)  
  28.         {  
  29.             Console.WriteLine("Method 'Print_Output' with one double type parameter whose value is: " + val);  
  30.         }  
  31.   
  32.         // Method with two parameter of int and double type  
  33.         public void Print_Output(int val, string str)  
  34.         {  
  35.             Console.WriteLine(string.Format("Method 'Print_Output' with two parameters whose value are: {0} {1} ", val, str));  
  36.         }  
  37.   
  38.         // Method with two parameter of double and int type. Here the parameters order are change with the previous one  
  39.         public void Print_Output(string str, int val)  
  40.         {  
  41.             Console.WriteLine(string.Format("Method 'Print_Output' with two parameters whose value are: {0} {1} ", str, val));  
  42.         }  
  43.   
  44.         // Method with three parameter and int return type  
  45.         public int Print_Output(double val, int int_Val, string name)  
  46.         {  
  47.             Console.WriteLine("Method 'Print_Output' with two parameters whose value are: " + val, " ", +int_Val);  
  48.             return int_Val;  
  49.         }  
  50.   
  51.         static void Main(string[] args)  
  52.         {  
  53.             Console.WriteLine("hello Tutpoint");  
  54.             Program program = new Program();  
  55.   
  56.             program.Print_Output();  
  57.             program.Print_Output(100);  
  58.             program.Print_Output(100.81);  
  59.             program.Print_Output("Kirti");  
  60.             program.Print_Output(100, "Deeksha");  
  61.             program.Print_Output("Monark", 0);  
  62.             int retVal = program.Print_Output(1, 1, "Pooja");  
  63.             Console.ReadLine();  
  64.         }  
  65.     }  
  66.   
  67. }  
Output of this program is shown below:
  1. hello Tutpoint  
  2. Method 'Print_Output' with no parameter  
  3. Method 'Print_Output' with one int type parameter whose value is: 100  
  4. Method 'Print_Output' with one double type parameter whose value is: 100.81  
  5. Method 'Print_Output' with one string type parameter whose value is: Kirti  
  6. Method 'Print_Output' with two parameters whose value are: 100 Deeksha  
  7. Method 'Print_Output' with two parameters whose value are: Monark 0  
  8. Method 'Print_Output' with two parameters whose value are: 1  
Note

When we add the two methods shown below in the above class then compiler will show an error,
  1.        /* Shows an error as no two methods with same name can have same parameters type and only diffenence in return type 
  2.        Error as "Type 'TutLovers' already defines a member called 'Print_Output' with the same parameter type" */  
  3.        public int Print_Output(string str, int val)  
  4.        {  
  5.            Console.WriteLine("Method 'Print_Output' with two parameters ");  
  6.            return val;  
  7.        }  
  8.   
  9.        /* Shows an error as no two methods with same name can have same parameters type and only diffenence in return type. 
  10.        Error as "Type 'TutLovers' already defines a member called 'Print_Output' with the same parameter type" */  
  11.        public int Print_Output()  
  12.        {  
  13.            Console.WriteLine("Method 'Print_Output' with no parameters ");  
  14.            return 0;  
  15.        }  
The Reason for the error is the same as described above. No two methods declared with the same name can have differences only in return type. These two methods are already implemented which has the same parameter's order and number.
  1. public void Print_Output(string str, int val)  
  2.         {  
  3.             Console.WriteLine(string.Format("Method 'Print_Output' with two parameters whose value are: {0} {1} ", str, val));  
  4.         }  
and
  1. public void Print_Output()  
  2. {  
  3. Console.WriteLine("Method 'Print_Output' with no parameter");  
  4. }  
Next Recommended Reading Remove AM PM from Time String using C#