C# Methods

Overloading in C# with Examples

In overloading, there are multiple methods with different method signatures.

Below are some examples that show how we can achieve overloading by varying the number of parameters, the order of parameters, and data types of parameters.

Example 1:

In the above example, there are two methods with the same name but a different number of parameters.  The first method consists of two parameters while the second one has three parameters. This is called method overloading.

Output:

Example 2:

In the above example, there are two methods with the same name but their data types are different. The first method has an integer data type while the second has a double data type. So in this case parameters are varying because of the different datatype.

Output:

Example 3:

In the above example, the name of the methods are the same but the order of parameters are different. The first method has a name and id resp. whereas the second one has id and name respectively.

Points to Remember:

In an overloading concept, it is not possible to define more than one method with the same parameters in case of order, type and number.

It is not possible to overload a method based on the different return types.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.