Method Overloading In C#

C# method overloading allows a class to declare multiple methods with the same name, separated by their signatures. This article explains method overloading and method overriding in C# with examples.

In this article, we will learn how to implement two common and reusable object oriented programming features of C# language, method overloading and method overriding. Here are some of the recommended articles if you're new to object oriented and C# programming.

Let's start with method overloading in C#.

What is  Method Overloading?
Method overloading allows programmers to use multiple methods with the same name. The methods are differentiated with their number and type of method arguments. Method overloading is an example of the polymorphism feature of an object oriented programming language.
Method overloading can be achieved by the following:
  • By changing number of parameters in a method
  • By changing the order of parameters in a method
  • By using different data types for parameters
Here is an example of method overloading. 
  1. public class Methodoveloading    
  2.   {    
  3.     public int add(int a, int b)  //two int type Parameters method  
  4.     {    
  5.         return a + b;    
  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;    
  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;    
  17.     }    
  18.   }   

In the above example, there are three methods with the same names but each method differs in number of parameters and also types of parameters. This method is called method overloading. One of the common example of method overloading is formatting a string using the Format method. The string can be formatted using various input types such as int, double, numbers, chars, and other strings.

FAQ of Method Overloading

Question: Can you overload a method based on a different return type?
Answer: No. Compiler does not allow just return type to seperate one method from other with same name.

What is Method overriding?

Method overriding in C# allows programmers to create base classes that allows its inherited classes to override same name methods when implementing in their class for different purpose. This method is also used to enforce some must implement features in derived classes.
Important points:
  • Method overriding is only possible in derived classes, not within the same class where the method is declared.
  • Base class must use the virtual or abstract keywords to declare a method. Then only can a method be overridden
Here is an example of method overriding.
  1. public class Account
  2.   {  
  3.     public virtual int balance()  
  4.     {  
  5.         return 10;  
  6.     }  
  7.   }  
  8. public class Amount:Account
  9. {  
  11.     public override int balance()  
  12.     {  
  13.         return 500;  
  14.     }  

In the above program, the result of the balance method can be 10 or 500.
The above code declares two classes, Account and Amount. The Amount class is inherited from the Account class. Method balance is overridden in the Amount class. The value of the balance method will be decided based on the caller program and its use of base class or derived class.


This article is a basic introduction of method overloading and method overriding in C#.