Thursday, May 17, 2018

C# : Method Overloading

Creating a multiple methods in a class with same name but different parameters and types is called as method overloading.method overloading is the example of Compile time polymorphism which is  done at compile time.

Method overloading can be achieved by using following things :
  • By changing the number of parameters used.
  • By changing the order of parameters.
  • By using different data types for the parameters.
  1. public class Methodoveloading    
  2.   {    
  3.     public int add(int a, int b)  //two int type Parameters method  
  4.     {    
  5.         return a + b;    
  6.         
  7.     }    
  8.     public int add(int a, int b,int c)  //three int type Parameters with same method same as above  
  9.     {    
  10.         return a + b+c;    
  11.     
  12.     }    
  13.     public float add(float a, float b,float c,float d)  //four float type Parameters with same method same as above two method 
  14.     {    
  15.         return a + b+c+d;    
  16.     
  17.     }    
  18.   }    
In the above example ,their are three methods with same method same but they differ with number of parameters and type of parameters ,hence this types of methods is called as method overloading.
 
FAQ of Method Overloading 
Question-    Can method overloading has same numbers of parameters and Name with different return types?
Answer-  No, because conflict is happen in methods while ing the parameters .

No comments:

Post a Comment