What is Method Overloading in C#.net

Method Overloading C#.net is a useful way of implementing polymorphism. It is the ability to define a function in multiple form. A user can implement function overloading by declaring two or more functions in a class having the same name. C#.net can identify 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.

  • Overloaded methods can be differentiated based on the number and its type of the parameters passed as arguments to the methods.
  • You can not define more than one method with the same name, Order and the type of the arguments. It is considered as compiler error.

Why do we need Method Overloading ??

If we have requirement to perform 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 parameter. It is hard to find many different meaningful names for single action. So in such a situation it is useful

Now Open Visual Studio and create Console Application then write below line of code in it.

using System; 
class DotNetByExample { 
  
    // adding two integer values. 
    public int Add(int no1, int no2) 
    { 
        int sum = no1 + no2; 
        return sum; 
    } 
  
    // adding three integer values. 
    public int Add(int no1 , int no2, int no3) 
    { 
        int sum = no1  + no2 + no3; 
        return sum; 
    } 
  
    // Main Method 
    public static void Main(String[] args) 
    { 
  
        // Creating Object 
        DotNetByExample ob = new DotNetByExample (); 
  
        int sum1 = ob.Add(5, 10); 
        Console.WriteLine("sum of the two "
                          + "integer value : " + sum1); 
  
        int sum2 = ob.Add(5, 10, 15); 
        Console.WriteLine("sum of the three "
                          + "integer value : " + sum2); 
    } 
} 

 

Summary

You can also read about ASP.NET,C#.Net,Jquery.
I hope you get an idea about What is Method Overloading in C#.net.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you liked this post, don’t forget to share this.