Codectionary / Developer documentation / C#

Method Overloading

Method overloading lets a class define multiple methods with the same name but different parameter lists, differing in the number, type, or order of parameters. C# resolves which overload to call at compile time based on the arguments provided. C# also supports optional parameters (with a default value) and named arguments, which can reduce the need for overloads in some cases.

Read the syntax

Overloading gives methods the same name but different parameter lists; the compiler selects a matching overload using argument count and types. A different return type alone does not distinguish an overload.

Optional parameters supply defaults when arguments are omitted. Named arguments identify parameters at the call site and can make calls with several optional settings easier to read.

Syntax

returnType MethodName(paramsA) { }\nreturnType MethodName(paramsB) { }

Examples

Overloading by Parameter Count and Type

Multiple versions of a method accepting different numbers or types of arguments.

Calculator calc = new Calculator();
Console.WriteLine(calc.Add(2, 3));        // 5
Console.WriteLine(calc.Add(2, 3, 4));      // 9
Console.WriteLine(calc.Add(2.5, 3.5));      // 6

public class Calculator
{
    public int Add(int a, int b) => a + b;
    public int Add(int a, int b, int c) => a + b + c;
    public double Add(double a, double b) => a + b;
}
  1. Multiple versions of a method accepting different numbers or types of arguments.
  2. Overloading gives methods the same name but different parameter lists; the compiler selects a matching overload using argument count and types. A different return type alone does not distinguish an overload.
  3. The two-int, three-int and two-double calls select different Add overloads. They print 5, 9 and 6 respectively.

Optional Parameters as an Alternative

C# often reduces the need for overloading with default parameter values.

Greeter g = new Greeter();
Console.WriteLine(g.Greet("John"));            // Hello, John!
Console.WriteLine(g.Greet("Zain", "Hey"));      // Hey, Zain!

public class Greeter
{
    public string Greet(string name, string greeting = "Hello")
    {
        return $"{greeting}, {name}!";
    }
}
  1. C# often reduces the need for overloading with default parameter values.
  2. Overloading gives methods the same name but different parameter lists; the compiler selects a matching overload using argument count and types. A different return type alone does not distinguish an overload.
  3. The first call omits greeting and receives Hello by default. The second supplies Hey explicitly, so the two greetings differ.

Named Arguments

Named arguments let you specify parameters by name, in any order, improving call-site readability.

Pizza pizza = new Pizza();
pizza.Order(size: "Large", extraCheese: true);  // toppingCount uses its default

public class Pizza
{
    public void Order(string size, int toppingCount = 0, bool extraCheese = false)
    {
        Console.WriteLine($"{size} pizza, {toppingCount} toppings, extra cheese: {extraCheese}");
    }
}
  1. Named arguments let you specify parameters by name, in any order, improving call-site readability.
  2. Overloading gives methods the same name but different parameter lists; the compiler selects a matching overload using argument count and types. A different return type alone does not distinguish an overload.
  3. The named call sets size and extraCheese while omitting toppingCount. That count remains 0 and the printed order includes extra cheese: True.

Best practices

  • Only overload methods when the different versions perform genuinely similar, related operations - otherwise use distinct method names for clarity
  • Consider optional parameters instead of overloading when the only difference between overloads is a default value for one extra parameter
  • Use named arguments at call sites for methods with several parameters, especially when skipping optional ones, to keep the call self-documenting
  • Avoid overloads that are ambiguous for the compiler to resolve, such as very similar numeric type combinations

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading