Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Sep 20, 2022, 4:37 AM EDT
Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name. C# can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters list (i.e. the number of the parameters, order of the parameters, and data types of the parameters) within the same class.
Why do we need Method Overloading?
If we need to do the same kind of the operation in different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many different meaningful names for single action.
Different ways of doing overloading methods-
Method overloading can be done by changing:
By changing the Number of Parameters
// C# program to demonstrate the function // overloading by changing the Number // of parameters using System; class DEMO{ // adding two integer values. public int Add(int a, int b) { int sum = a + b; return sum; } // adding three integer values. public int Add(int a, int b, int c) { int sum = a + b + c; return sum; } // Main Method public static void Main(String[] args) { // Creating Object DEMO ob = new DEMO(); int sum1 = ob.Add(1, 2); Console.WriteLine("sum of the two " + "integer value : " + sum1); int sum2 = ob.Add(1, 2, 3); Console.WriteLine("sum of the three " + "integer value : " + sum2); } }Output:
sum of the two integer value : 3
sum of the three integer value : 6
By changing the Data types of the parameters
// C# program to demonstrate the function // overloading by changing the // Order of the parameters using System; class DEMO { // Method public void Identity(String name, int id) { Console.WriteLine("Name1 : " + name + ", " + "Id1 : " + id); } // Method public void Identity(int id, String name) { Console.WriteLine("Name2 : " + name + ", " + "Id2 : " + id); } // Main Method public static void Main(String[] args) { // Creating Object DEMO obj = new DEMO(); obj.Identity("Uday", 1); obj.Identity(2, "Jayesh"); } }Output:
Name1 : Uday, Id1 : 1
Name2 : Jayesh, Id2 : 2
What happens when method signature is same and the return type is different?
The compiler will give error as the return value alone is not sufficient for the compiler to figure out which function it has to call. Only if both methods have different parameter types (so, they have the different signature), then Method overloading is possible.
Example:
// C# program to show error when method signature // is the same and the return type is different. using System; class DEMO{ // adding two integer value. public int Add(int a, int b) { int sum = a + b; return sum; } // adding three integer value. public double Add(int a, int b) { double sum = a + b + 0.0; return sum; } // Main Method public static void Main(String[] args) { // Creating Object DEMO ob = new DEMO(); int sum1 = ob.Add(1, 2); Console.WriteLine("sum of the two " + "integer value :" + sum1); int sum2 = ob.Add(1, 2); Console.WriteLine("sum of the three " + "integer value :" + sum2); } }Compile Time Error:
prog.cs(15,19): error CS0111: A member DEMO.Add(int, int)’ is already defined. Rename this member or use different parameter types
prog.cs(7,16): (Location of the symbol related to previous error)
Vishal YelveRank #178 · 13.6K reputationPosted Sep 20, 2022, 3:11 AM EDT
Method Overloading - A method with the same name but different numbers, types, and order of parameters, it is called method overloading.
Method overloading can be achieved by the following:
public class Methodoveloading { public int add(int a, int b) //two int type Parameters method { return a + b; } public int add(int a, int b,int c) //three int type Parameters with same method same as above { return a + b+c; } public float add(float a, float b,float c,float d) //four float type Parameters with same method same as above two method { return a + b+c+d; } }Rajanikant HawaldarRank #110 · 20.2K reputationPosted Sep 20, 2022, 7:12 AM EDT
Hello Naresh,
Refer below article
https://www.c-sharpcorner.com/UploadFile/0c1bb2/method-oveloading-and-overriding-C-Sharp/
Amit KumarRank #459 · 5.4K reputationPosted Sep 20, 2022, 9:33 AM EDT
Hello,
Method Overloading l simple Code
void Area (float r);
void Area(float l, float b);
void Area(float a, float b, float c);
using System; namespace method_overloading_polymorphism { Class Program { Public class Shape { Public void Area (float r) { float a = (float)3.14 * r; // here we have used function overload with 1 parameter. Console.WriteLine ("Area of a circle: {0}",a); } Public void Area(float l, float b) { float x = (float)l* b; // here we have used function overload with 2 parameters. Console.WriteLine ("Area of a rectangle: {0}",x); } public void Area(float a, float b, float c) { float s = (float)(a*b*c)/2; // here we have used function overload with 3 parameters. Console.WriteLine ("Area of a circle: {0}", s); } } Static void Main (string[] args) { Shape ob = new Shape (); ob.Area(2.0f); ob.Area(20.0f,30.0f); ob.Area(2.0f,3.0f,4.0f); Console.ReadLine (); } } }Nitin SontakkeRank #264 · 9.6K reputationPosted Sep 20, 2022, 5:22 AM EDT
Further to all answers already provided, in my opinion, the body (the code) of the method should not be duplicated in case of method overloading.
Given below are two separate implementations of sample code given by Vishal Yelve.
public class MethodOverloading { public int add(int a, int b) //two int type Parameters method { return a + b; } public int add(int a, int b,int c) //three int type Parameters with same method same as above { int result = add(a, b); return add(result, c); } }Or
public class MethodOverloading { public int add(int a, int b) //two int type Parameters method { return add(a, b, 0); } public int add(int a, int b,int c) //three int type Parameters with same method same as above { return a + b + c; } }Hope I have managed to convey the point correctly.
Hope it helps!
Sachin SinghRank #66 · 26.2K reputationPosted Sep 20, 2022, 5:28 AM EDT
We create overloaded version of a method, when we want to perform different actions based on the method signature.
For example, C# extension methods on Collections (IEnumerable) like Sort() methods has 4 overloaded versions
Sorts the elements in the entire List using the specified Comparison.
Sorts the elements in a range of elements in List using the specified comparer.
Sorts the elements in the entire List using the default comparer.
Sorts the elements in the entire List using the specified comparer.
So, microsoft heavily use the method overloading concept, similarly you can use as well.
Abhishek YadavRank #217 · 11.3K reputationPosted Oct 19, 2022, 4:37 AM EDT
Refer below article
https://www.c-sharpcorner.com/UploadFile/0c1bb2/method-oveloading-and-overriding-C-Sharp/
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Oct 18, 2022, 4:19 AM EDT
Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name. C# can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters list (i.e. the number of the parameters, order of the parameters, and data types of the parameters) within the same class.
Why do we need Method Overloading?
If we need to do the same kind of the operation in different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many different meaningful names for single action.
Different ways of doing overloading methods-
Method overloading can be done by changing:
usingSystem;classCSharpCorner{// adding two integer values.publicintAdd(inta,intb){intsum = a + b;returnsum;}// adding three integer values.publicintAdd(inta,intb,intc){intsum = a + b + c;returnsum;}// Main MethodpublicstaticvoidMain(String[] args){// Creating ObjectCSharpCornerob = newCSharpCorner();intsum1 = ob.Add(1, 2);Console.WriteLine("sum of the two "+"integer value : "+ sum1);intsum2 = ob.Add(1, 2, 3);Console.WriteLine("sum of the three "+"integer value : "+ sum2);}}Output: