Method Overloading In C#

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. 

Let's start with method overloading in C#.

What is Method Overloading in C#?

Method overloading allows programmers to use multiple methods with the same name. The methods are differentiated by 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 the 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.

public class Methodoveloading
{
    public int add(int a, int b)  //two int type Parameters method
    {
        return a + b;
    }
    public int add(int a, int b,int c)  //three int type Parameters with same method same as above
    {
        return a + b+c;
    }
    public float add(float a, float b,float c,float d)  //four float type Parameters with same method same as above two method
    {
        return a + b+c+d;
    }
}

In the above example, there are three methods with the same names, but each method differs in the number of parameters and types of parameters. This method is called method overloading. One common example of method overloading is formatting a string using the Format method. The string can be formatted using 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. The compiler does not allow just return type to separate one method from other with the same name.

What is method overriding in C#?

Method overriding in C# allows programmers to create base classes that will enable its inherited classes to override same name methods when implementing in their class for a different purpose. This method also enforces 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.
  • The base class must use virtual or abstract keywords to declare a method. Then only can a method be overridden

Here is an example of method overriding.

public class Account
{  
    public virtual int balance()  
    {  
        return 10;  
    }  
}  
public class Amount:Account
{   
    public override int balance()  
    {  
        return 500;  
    }  
}

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.  

Summary

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


Recommended Free Ebook
Similar Articles