Advantages of method Overloading in C#.Net

By | November 19, 2017

Advantages of method Overloading in C#.Net Or When/Why to Use Method Overloading in C#.Net

Suppose you get one program to create a method which can do sum of two numbers, three numbers & four numbers by passing parameters.

So you will end up writing following methods,

Public void SumTwoNumbers(int a, int b)
{
}

Public void SumThreeNumbers(int a, int b, int c)
{
}

Public void SumFourNumbers(int a, int b, int c, int d)
{
}

And what if, if program ask you to do sum of upto 20 numbers, so you will end up with writing 20 different methods, and most worst task is the user who will use your program he/she might needs to use remember all the methods name.

So to overcome this scenario microsoft indroduces Method overloading.

So in this case you will write 20 methods with the same name but having different method signature, so here user don’t need to remember all the methods name, he just need to remember single method name as sum.

Also the example explains above contains only sum of integer number only and what if it should allow flaot and double numbers too. so in this case you have to remember many methods name, but in case of method overloading you just need to remember one name, so visual studio intelligence show you all the overloaded methods as below,

Advantages of method Overloading in C#.Net

Advantages of method Overloading in C#.Net

 

So in short,

  • Method Overloading improves code readability & re-usability.
  • Save efforts & memory. (it will save only one method name in memory instead of all methods like sumoftwonumbers etc)
Polymorphism in .Net and Types of Polymorphism in .Net

Leave a Reply

Your email address will not be published. Required fields are marked *