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.

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.

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;
}

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

Optional Parameters as an Alternative

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

public class Greeter
{
    public string Greet(string name, string greeting = "Hello")
    {
        return $"{greeting}, {name}!";
    }
}

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

Named Arguments

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

public class Pizza
{
    public void Order(string size, int toppingCount = 0, bool extraCheese = false)
    {
        Console.WriteLine($"{size} pizza, {toppingCount} toppings, extra cheese: {extraCheese}");
    }
}

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

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

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Classes & Objects
A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.
Constructors
A constructor is a special method that runs automatically when an object is created with new, used to initialize the object's state. A constructor shares its name with the class and has no return type. C# supports constructor overloading (multiple constructors with different parameter lists) and constructor chaining with this(...), where one constructor calls another in the same class to avoid duplicating initialization logic.
Properties (get/set)
Properties are C#'s idiomatic way to expose class data through accessor-like syntax while still allowing controlled access via get and set. Unlike a plain public field, a property can validate a value before it's set, compute a value on the fly, or restrict access to read-only. Auto-implemented properties (using { get; set; } with no body) provide a concise shorthand when no custom logic is needed.
Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and properties from other parts of a program. C# provides public (accessible from anywhere), private (accessible only within the declaring class - the default for class members), protected (accessible within the class and its subclasses), internal (accessible only within the same assembly/project), and combinations like protected internal and private protected.