Create a method overloading with example
Loading
Create a method overloading with example
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Uday DodiyaPosted Sep 20, 2022, 8:37 AM
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
Output:
sum of the two integer value : 3
sum of the three integer value : 6
By changing the Data types of the parameters
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:
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)
Abhishek YadavPosted Oct 19, 2022, 8:37 AM
Refer below article
https://www.c-sharpcorner.com/UploadFile/0c1bb2/method-oveloading-and-overriding-C-Sharp/
Brahma Prakash ShuklaPosted Oct 18, 2022, 8:19 AM
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:
Amit KumarPosted Sep 20, 2022, 1:33 PM
Hello,
Method Overloading l simple Code
void Area (float r);
void Area(float l, float b);
void Area(float a, float b, float c);
Rajanikant HawaldarPosted Sep 20, 2022, 11:12 AM
Hello Naresh,
Refer below article
https://www.c-sharpcorner.com/UploadFile/0c1bb2/method-oveloading-and-overriding-C-Sharp/
Sachin SinghPosted Sep 20, 2022, 9:28 AM
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.
Nitin SontakkePosted Sep 20, 2022, 9:22 AM
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.
Or
Hope I have managed to convey the point correctly.
Hope it helps!
Vishal YelvePosted Sep 20, 2022, 7:11 AM
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: