C# Method Overloading
Method overloading in C# allows a class to contain multiple methods with the same name, provided that their parameter lists are different. The methods may accept a different number of parameters, different parameter types, or parameters in a different order.
Method overloading is an example of compile-time polymorphism. The C# compiler selects the appropriate method by examining the arguments used in the method call.
C# Method Overloading Syntax
The following syntax shows two overloaded methods. Both methods have the same name, but their parameter lists are different.
returnType methodName(parameterType1 parameter1)
{
// Method body
}
returnType methodName(parameterType1 parameter1, parameterType2 parameter2)
{
// Method body
}
The return type is not used to distinguish overloaded methods. Each overload must have a unique method signature based on its method name and parameter list.
Overload a C# Method with a Different Number of Arguments
In the following example, the Calculator class defines two add() methods. One accepts two integer arguments, while the other accepts three integer arguments.
Program.cs
using System;
namespace CSharpExamples {
class Calculator{
public int add(int a, int b){
Console.WriteLine("Adding two numbers..");
return a+b;
}
public int add(int a, int b, int c){
Console.WriteLine("Adding three numbers..");
return a+b+c;
}
}
class Program {
static void Main(string[] args) {
int a = 10;
int b = 5;
int c = 7;
Calculator calc = new Calculator();
int sum1 = calc.add(a,b);
Console.WriteLine("a+b = " + sum1);
int sum2 = calc.add(a,b,c);
Console.WriteLine("a+b+c = " + sum2);
}
}
}
When the program calls calc.add(a, b), the compiler selects the overload that accepts two integer parameters.
When the program calls calc.add(a, b, c), the compiler selects the overload that accepts three integer parameters.
Output
Adding two numbers..
a+b = 15
Adding three numbers..
a+b+c = 22
Overload a C# Method with Different Parameter Types
A method can also be overloaded by changing the data types of its parameters. In the following example, one add() method accepts two int values, while the other accepts an int and a float.
Program.cs
using System;
namespace CSharpExamples {
class Calculator{
public int add(int a, int b){
Console.WriteLine("Adding two integers..");
return a+b;
}
public float add(int a, float c){
Console.WriteLine("Adding integer and float..");
return a+c;
}
}
class Program {
static void Main(string[] args) {
int a = 10;
int b = 5;
float c = 7.4F;
Calculator calc = new Calculator();
int sum1 = calc.add(a,b);
Console.WriteLine("a+b = " + sum1);
float sum2 = calc.add(a,c);
Console.WriteLine("a+b+c = " + sum2);
}
}
}
For calc.add(a, b), both arguments are integers, so the overload with two int parameters is executed.
For calc.add(a, c), the arguments are an int and a float, so the overload with matching parameter types is executed.
Output
Adding two integers..
a+b = 15
Adding integer and float..
a+b+c = 17.4
Overload a C# Method by Changing Parameter Order
Two overloads may contain the same parameter types when those types appear in a different order. The order must make the method signatures distinct.
using System;
class MessageFormatter
{
public void Display(string message, int repeatCount)
{
for (int i = 0; i < repeatCount; i++)
{
Console.WriteLine(message);
}
}
public void Display(int code, string message)
{
Console.WriteLine($"{code}: {message}");
}
}
class Program
{
static void Main()
{
MessageFormatter formatter = new MessageFormatter();
formatter.Display("Hello", 2);
formatter.Display(200, "Success");
}
}
Output
Hello
Hello
200: Success
How the C# Compiler Selects an Overloaded Method
During compilation, C# performs overload resolution. It evaluates the available methods and selects the best match for the supplied arguments.
- The number of arguments must match.
- The argument types must match the parameter types or support an allowed implicit conversion.
- The argument order must correspond to the parameter order.
- When multiple overloads are applicable, the compiler chooses the most specific valid match.
- If no single best match exists, compilation fails with an ambiguous-call error.
Method Overloading Cannot Depend Only on Return Type
Changing only the return type does not create a valid overload. The following methods have identical parameter lists and therefore produce a compiler error.
public int GetValue(int number)
{
return number;
}
// Invalid: return type alone does not create a new overload.
public double GetValue(int number)
{
return number;
}
A method signature used for overloading includes the method name and its parameter types, modifiers, and order. It does not include the return type.
C# Method Overloading with ref, out, and in Parameters
Parameter modifiers affect overload signatures, but some combinations require care. A method using a value parameter can be overloaded with one using ref, out, or in. However, methods cannot be overloaded when the only difference is ref versus out, because C# treats those signatures as equivalent for this rule.
public void Update(int value)
{
Console.WriteLine(value);
}
public void Update(ref int value)
{
value++;
}
Method Overloading with Optional Parameters
Optional parameters can make overloaded calls ambiguous. Consider the following methods:
public void Print(string text)
{
Console.WriteLine(text);
}
public void Print(string text, int copies = 1)
{
for (int i = 0; i < copies; i++)
{
Console.WriteLine(text);
}
}
A call such as Print("Hello") can match both methods. Avoid overload sets in which optional parameters create unclear or competing matches.
Method Overloading and Method Overriding in C#
| Method overloading | Method overriding |
|---|---|
| Uses the same method name with different parameter lists. | Replaces an inherited virtual or abstract method implementation. |
| Usually occurs within the same class, although inherited overloads can also participate. | Requires a base class and a derived class. |
| Resolved mainly at compile time. | Resolved at runtime for virtual calls. |
Does not require virtual or override. | Uses modifiers such as virtual, abstract, and override. |
When to Use Method Overloading in C#
Use method overloading when several operations perform the same conceptual task but accept different forms of input. Common examples include constructors, numeric calculations, formatting methods, parsing operations, and methods that accept either a single item or a collection.
- Keep all overloads consistent in purpose and behavior.
- Use clear parameter names so each overload is easy to understand.
- Avoid overloads that differ only through confusing implicit conversions.
- Do not create too many overloads when an options object or a separate method name would be clearer.
- Check calls involving
null, optional parameters, numeric conversions, and generic types for ambiguity.
Common C# Method Overloading Errors
- Using only a different return type: The parameter lists must also differ.
- Creating an ambiguous call: More than one overload may be an equally valid match.
- Confusing argument expressions: Method arguments are separated with commas, as in
add(a, b), not combined asadd(a + b). - Relying on unexpected numeric conversions: An integer argument may be convertible to several numeric parameter types.
- Mixing unrelated behavior: Overloads should represent variations of the same operation.
C# Method Overloading FAQs
What is method overloading in C#?
Method overloading is the declaration of multiple methods with the same name but different parameter lists. The compiler selects the appropriate overload from the arguments supplied by the caller.
Is method overloading compile-time polymorphism?
Yes. Method overloading is commonly described as compile-time or static polymorphism because overload resolution normally occurs during compilation.
Can overloaded methods have different return types?
Yes, overloaded methods may have different return types, but their parameter lists must also differ. Return type alone cannot distinguish two overloads.
Can static methods be overloaded in C#?
Yes. Static methods can be overloaded using the same parameter-list rules as instance methods.
Can constructors be overloaded in C#?
Yes. A class can define multiple constructors with different parameter lists, allowing objects to be initialized in different ways.
C# Method Overloading Summary
Method overloading lets you reuse a method name for related operations that require different inputs. Valid overloads differ by the number, type, modifier, or order of their parameters. The compiler determines which overload to call, while the return type alone does not form a distinct overload.
In this C# Tutorial, we learned how C# method overloading works, how the compiler resolves overloaded calls, and which signature rules and ambiguity cases to consider.
TutorialKart.com