What is the method overloading with example?
Loading
What is the method overloading with example?
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
Amit MohantyRank #112 · 20K reputationPosted Apr 26, 2023, 8:42 AM EDT
Method overloading is a concept in object-oriented programming where multiple methods can have the same name but different parameters. In ASP.NET, method overloading allows you to define several methods with the same name in a class, but each method has a different set of parameters.
Here's an example of method overloading in ASP.NET:
public class Calculator { public int Add(int x, int y) { return x + y; } public int Add(int x, int y, int z) { return x + y + z; } }In the above example, we have defined a class called
Calculator. It contains two methods with the same nameAdd, but different parameters. The firstAddmethod takes two integers as parameters and returns the sum of them. The secondAddmethod takes three integers as parameters and returns the sum of them.By using method overloading, we can make our code more readable and easier to maintain. In this case, we can use the same method name
Addfor both two and three integer arguments instead of creating separate methods for each case.Vishal YelveRank #178 · 13.6K reputationPosted Apr 26, 2023, 10:05 AM EDT
Method Overloading is a type of polymorphism. It has several names like “Compile Time Polymorphism” or “Static Polymorphism,” and sometimes it is called “Early Binding”.
Method Overloading means creating multiple methods in a class with the same names but different signatures (Parameters). It permits a class, struct, or interface to declare multiple methods with the same name with unique signatures.
The compiler automatically calls the required method to check the number of parameters and their type passed into that method.
class Program { public int Add(int num1, int num2) { return (num1 + num2); } public int Add(int num1, int num2, int num3) { return (num1 + num2 + num3); } public float Add(float num1, float num2) { return (num1 + num2); } public string Add(string value1, string value2) { return (value1 + " " + value2); } static void Main(string[] args) { Program objProgram = new Program(); Console.WriteLine("Add with two int parameter :" + objProgram.Add(3, 2)); Console.WriteLine("Add with three int parameter :" + objProgram.Add(3, 2, 8)); Console.WriteLine("Add with two float parameter :" + objProgram.Add(3 f, 22 f)); Console.WriteLine("Add with two string parameter :" + objProgram.Add("hello", "world")); Console.ReadLine(); }