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
- {
- // Method with no parameter
- public void Print_Output()
- {
- Console.WriteLine("Method 'Print_Output' with no parameter");
- }
- // Method with one parameter of int type
- public void Print_Output(int val)
- {
- Console.WriteLine("Method 'Print_Output' with one int type parameter whose value is: " + val);
- }
- // Method with one parameter of string type
- public void Print_Output(string val)
- {
- Console.WriteLine("Method 'Print_Output' with one string type parameter whose value is: " + val);
- }
- // Method with one parameter of double type
- public void Print_Output(double val)
- {
- Console.WriteLine("Method 'Print_Output' with one double type parameter whose value is: " + val);
- }
- // Method with two parameter of int and double type
- 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));
- }
- // Method with two parameter of double and int type. Here the parameters order are change with the previous one
- 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));
- }
- // Method with three parameter and int return type
- 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();
- }
- }
- }

flip divingPosted Oct 10, 2017, 11:18 PM
Thank you for your sharing. Thanks to this article I can learn more things. Expand your knowledge and abilities. Actually the article is very practical. Thank you!