Method Overloading says, when two or more methods have the same name but different parameters.
Below are two types of difference in parameters that C# considers for method overloading :
Now, let's see the first way of operator Overloading.
int add(int firstNumber, int secondNumber, int thirdNumber)
int add(int firstNumber, int secondNumber)
int add(int firstNumber)
public class MyApplication { public static void Main(string[] args) { int firstNum = 5; int secondNum = 8; int thirdNum = 2; int addedResult1; int addedResult2; addedResult1 = add(firstNum,secondNum); System.Console.WriteLine("The added value of two numbers is : "+addedResult1); addedResult2 = add(firstNum,secondNum,thirdNum); System.Console.WriteLine("The added value of three numbers is : "+addedResult2); } //Add method starts here. static int add(int firstNumber, int secondNumber) { int result; result = firstNumber + secondNumber; return result; } //Overloaded Add method starts here. static int add(int firstNumber, int secondNumber, int thirdNumber) { int result; result = firstNumber + secondNumber + thirdNumber; return result; } }
addedResult1 = add(firstNum,secondNum);
addedResult2 = add(firstNum,secondNum,thirdNum);
float add(float firstNumber, float secondNumber)
int add(int firstNumber, int secondNumber)
public class MyApplication { public static void Main(string[] args) { int firstNum = 5; int secondNum = 8; float firstNumFloat = 2.4F; float secondNumFloat = 9.5F; int addedResult1; float addedResult2; addedResult1 = add(firstNum,secondNum); System.Console.WriteLine("The added value of two numbers : "+addedResult1); addedResult2 = add(firstNumFloat,secondNumFloat); System.Console.WriteLine("The added value of decimal numbers : "+addedResult2); } //Add method starts here. static int add(int firstNumber, int secondNumber) { int result; result = firstNumber + secondNumber; return result; } //Overloaded Add method starts here. static float add(float firstNumber, float secondNumber) { float result; result = firstNumber + secondNumber; return result; } }
int add(int firstNumber, int secondNumber)
float add(float firstNumber, float secondNumber)
addedResult1 = add(firstNum,secondNum);
int add(int firstNumber, int secondNumber)
addedResult2 = add(firstNumFloat,secondNumFloat);
float add(float firstNumber, float secondNumber)