Table of contents
1.
Introduction
2.
C# Method Overloading
2.1.
Features Of Method Overloading
2.2.
Why do we need Method Overloading?
3.
Different ways of doing overloading methods
3.1.
By changing the Number of Parameter
3.2.
C#
3.3.
By changing the Data types of the parameters
3.4.
C#
3.4.1.
Output
3.5.
By changing the Order of the parameters
3.6.
C#
3.6.1.
Output
4.
Frequently Asked Questions
4.1.
What is the overloading method in C#?
4.2.
What is method overriding in C#?
4.3.
What is the method overloading concept?
5.
Conclusion
Last Updated: Nov 7, 2024
Easy

Method Overloading in C#

Author Akash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C#, method overloading is a powerful feature that enables developers to create multiple methods with the same name but with different parameters. This allows the same method name to perform varied tasks depending on the input, making the code more flexible, readable, and efficient. By utilizing method overloading, developers can simplify complex operations and make code more intuitive for users. In this blog, we’ll dive into the basics of method overloading in C#.

Method Overloading in C#

C# Method Overloading

Method Overloading is one of the efficient ways of implementing polymorphism. It's the ability to rewrite a function in multiple ways. A user can implement function overloading by specifying two or more functions in a class with the same name. C# can tell the difference between methods with various signatures. Methods with the same name but various parameter lists (number of parameters, Order of parameters, and data types of parameters) can exist within the same class.

Features Of Method Overloading

  • The number and type of parameters supplied as arguments to overloaded methods are used to distinguish them.
  • There can't be more than one Method with the same name, Order, or type of parameters. It'd be a compiler flaw.
  • When distinguishing the overloaded Method, the compiler ignores the return type. However, two methods with the same signature but different return types cannot be declared. There will be a compilation error. It is not possible if both methods have the same parameter types but distinct return types.

Why do we need Method Overloading?

If we try to define several methods with the same name and number of parameters, the compiler will throw an error. Method overloading has the advantage of making code more readable and maintainable.

Different ways of doing overloading methods

The three ways through which Method overloading can be executed arre: 

  •  By changing the number of parameters.
  •  By changing the parameters’ data types.
  •  By changing the parameters’ Order.

By changing the Number of Parameter

  • C#

C#

// function Overloading by changing the number of parameters
using System;
class Coding Ninjas Studio {

   // adding two integer values.
   public int func_add(int a, int b)
   {
       int sum = a + b;
       return sum;
   }

   // adding three integer values.
   public int func_add(int a, int b, int c)
   {
       int sum = a + b + c;
       return sum;
   }

   public static void Main(String[] args)
   {

       // Creating the Object
       Coding Ninjas Studio ob = new Coding Ninjas Studio();

       int sum1 = ob.func_add(1, 2);
       Console.WriteLine("sum of the two "
                         + "integer value : " + sum1);

       int sum2 = ob.func_add(1, 2, 3);
       Console.WriteLine("sum of the three "
                         + "integer value : " + sum2);
   }
}

 Output

sum of the two integer value : 3
sum of the three integer value : 6 

By changing the Data types of the parameters

  • C#

C#

using System;
class Coding Ninjas Studio {

   // adding three integer values.
   public int func_add(int a, int b, int c)
   {
       int sum = a + b + c;
       return sum;
   }

   // adding three double values.
   public double func_add(double a,
                     double b, double c)
   {
       double sum = a + b + c;
       return sum;
   }

   public static void Main(String[] args)
   {

       // Creating Object
       Coding Ninjas Studio ob = new Coding Ninjas Studio();

       int sum2 = ob.func_add(1, 2, 3);
       Console.WriteLine("sum of the three "
                         + "integer value : " + sum2);
       double sum3 = ob.func_add(1.0, 2.0, 3.0);
       Console.WriteLine("sum of the three "
                         + "double value : " + sum3);
   }
}

Output

sum of the three integer value : 6
sum of the three double value : 6

By changing the Order of the parameters

  • C#

C#

using System;
class Coding Ninjas Studio {

   // Method
   public void Identity(String name, int id)
   {

       Console.WriteLine("Name1 : " + name + ", "
                       + "Id1 : " + id);
   }
   
   // Method
   public void Identity(int id, String name)
   {

       Console.WriteLine("Name2 : " + name + ", "
                       + "Id2 : " + id);
   }

   // Main Method
   public static void Main(String[] args)
   {

       // Creating Object
       Coding Ninjas Studio obj = new Coding Ninjas Studio();

       obj.Identity("Akash", 1);
       obj.Identity(2, "John");
   }
}

Output

Name1 : Akash, Id1 : 1
Name2 : John, Id2 : 2


Check out this problem - Subarray Sum Divisible By K

Frequently Asked Questions

What is the overloading method in C#?

Method overloading in C# is the ability to define multiple methods with the same name but different parameter lists within the same class.

What is method overriding in C#?

Method overriding in C# allows a subclass to provide a specific implementation of a method already defined in its base class using the override keyword.

What is the method overloading concept?

The method overloading concept allows methods to perform different tasks based on varied parameter lists, improving flexibility and readability within a single class.

Conclusion

In this article, we have extensively discussed Method Overloading in C#. We have also discussed different ways of overloading functions in C#. We hope this blog has helped you to brush up your knowledge regarding the C#. Some official documentation on big data that can help you improve your understanding is C# Generics and C# Inheritance.

If you would like to gain more knowledge about C#, check out our articles on Operator Keyword in C#cloud platform comparison, and 10 AWS best books.  Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Live masterclass