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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
-
- public void Print_Output()
- {
- Console.WriteLine("Method 'Print_Output' with no parameter");
- }
-
-
- public void Print_Output(int val)
- {
- Console.WriteLine("Method 'Print_Output' with one int type parameter whose value is: " + val);
-
- }
-
-
- public void Print_Output(string val)
- {
- Console.WriteLine("Method 'Print_Output' with one string type parameter whose value is: " + val);
- }
-
-
- public void Print_Output(double val)
- {
- Console.WriteLine("Method 'Print_Output' with one double type parameter whose value is: " + val);
- }
-
-
- public void Print_Output(int val, string str)
- {
- Console.WriteLine(string.Format("Method 'Print_Output' with two parameters whose value are: {0} {1} ", val, str));
- }
-
-
- public void Print_Output(string str, int val)
- {
- Console.WriteLine(string.Format("Method 'Print_Output' with two parameters whose value are: {0} {1} ", str, val));
- }
-
-
- public int Print_Output(double val, int int_Val, string name)
- {
- Console.WriteLine("Method 'Print_Output' with two parameters whose value are: " + val, " ", +int_Val);
- return int_Val;
- }
-
- static void Main(string[] args)
- {
- Console.WriteLine("hello Tutpoint");
- Program program = new Program();
-
- program.Print_Output();
- program.Print_Output(100);
- program.Print_Output(100.81);
- program.Print_Output("Kirti");
- program.Print_Output(100, "Deeksha");
- program.Print_Output("Monark", 0);
- int retVal = program.Print_Output(1, 1, "Pooja");
- Console.ReadLine();
- }
- }
-
- }
Output of this program is shown below:
- hello Tutpoint
- Method 'Print_Output' with no parameter
- Method 'Print_Output' with one int type parameter whose value is: 100
- Method 'Print_Output' with one double type parameter whose value is: 100.81
- Method 'Print_Output' with one string type parameter whose value is: Kirti
- Method 'Print_Output' with two parameters whose value are: 100 Deeksha
- Method 'Print_Output' with two parameters whose value are: Monark 0
- 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,
-
-
- public int Print_Output(string str, int val)
- {
- Console.WriteLine("Method 'Print_Output' with two parameters ");
- return val;
- }
-
-
-
- public int Print_Output()
- {
- Console.WriteLine("Method 'Print_Output' with no parameters ");
- return 0;
- }
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.
- public void Print_Output(string str, int val)
- {
- Console.WriteLine(string.Format("Method 'Print_Output' with two parameters whose value are: {0} {1} ", str, val));
- }
and
- public void Print_Output()
- {
- Console.WriteLine("Method 'Print_Output' with no parameter");
- }