Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - METHOD OVERLOADING


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 :

  1. The number of parameters are different in two or more methods.

  2. The number of parameters are same in two or more methods but have different datatypes.


Now, let's see the first way of operator Overloading.

  1. The number of parameters are different in two or more methods.

    1. Say there is an add method with three parameters :



      int add(int firstNumber, int secondNumber, int thirdNumber)

    2. Also there is an add method with two parameters :



      int add(int firstNumber, int secondNumber)

    3. Also, there is an add method with just one parameter :



      int add(int firstNumber)


    All the above three methods are said to be overloaded methods and C# would treat them as separate methods.

    So, let us try adding 2 numbers with add method. Then try adding 3 numbers with the overloaded add method.

    Example :



    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;
    	}
    }
    


    Output :



      The added value of two numbers is : 13
      The added value of three numbers is : 15


    In the main method, at first, we have called the add method with two parameters :

    addedResult1 = add(firstNum,secondNum);


    Then we have called the add method with three parameters :

    addedResult2 = add(firstNum,secondNum,thirdNum);


    And C# treated both the methods as different methods, since they were overloaded.

    Next, let us look at the second way of Operator Overloading.

  2. The number of parameters are same in two or more methods but have different datatypes.

    1. Say there is an add method with two parameters :



      float add(float firstNumber, float secondNumber)

    2. Also there is an add method with two parameters :



      int add(int firstNumber, int secondNumber)


    Since, the datatype of the parameters in two methods do not match(i.e. Float and Int). The methods are said to be overloaded and C# would treat them as separate methods.

    Example :



    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;
    	}
    }
    


    Output :



      The added value of two numbers : 13
      The added value of decimal numbers : 11.9


    So, in the above code we have two methods with the same name but parameters are of different Data Types.

    int add(int firstNumber, int secondNumber)


    And

    float add(float firstNumber, float secondNumber)


    So, when the method is called with two Integer values,

    addedResult1 = add(firstNum,secondNum);


    The method with two Int parameteres is called,

    int add(int firstNumber, int secondNumber)


    Similarly, when the method is called with two Float values,

    addedResult2 = add(firstNumFloat,secondNumFloat);


    The method with two Float parameteres is called,

    float add(float firstNumber, float secondNumber)