skip to content

In C#, why does a class that declares both `int Parse(string text)` and `long Parse(string text)` fail to compile, and what makes two methods valid overloads?

level: juniorimportance: must knowfreq 62%

answer

  1. what the compiler compares
  2. the return type is left out
  3. parameter types, not parameter names
  4. CS0111 on the second declaration

basics

~20 s

Overloads must differ in signature: the parameter count and types, a parameter's by-reference kind, or the number of type parameters. Return type, parameter names, default values and params are not part of it, so the second Parse is error CS0111.

solid answer

~50 s

C# identifies a method by its **signature**: the name, the number of type parameters, and each parameter's type and whether it is passed by value or by reference. The return type is not in it, so two `Parse(string)` methods that differ only by returning `int` or `long` collide, and the second is error CS0111: "Type 'ReadingParser' already defines a member called 'Parse' with the same parameter types". Parameter names, default values and the `params` modifier do not count either, and neither do two spellings of one type: `string` vs `string?`, or `object` vs `dynamic`. Legal overloads change the parameter list, as `Parse(string text, int radix)` does, or add a type parameter, as `Parse<T>(string text)` does. A by-value parameter may overload a `ref` one, but methods that differ only between `ref`, `in` and `out` are error CS0663.

code

csharp · 16 lines
csharp
var parser = new ReadingParser();
Console.WriteLine(parser.Parse("42"));
Console.WriteLine(parser.Parse("42", 16));
Console.WriteLine(parser.Parse<long>("42"));

class ReadingParser
{
    public int Parse(string text) => int.Parse(text);

    // Uncomment for error CS0111: same name, same parameter types, only the return type differs.
    // public long Parse(string text) => long.Parse(text);

    // Legal overloads: a different parameter list, or a different number of type parameters.
    public int Parse(string text, int radix) => Convert.ToInt32(text, radix);
    public T Parse<T>(string text) where T : IParsable<T> => T.Parse(text, null);
}

go deeper

for a junior

Recall that overloads must differ in their parameter list, not their return type, and that the duplicate is error CS0111.

for a middle

Explain what the signature contains and why the caller's arguments alone drive resolution, then list the near-misses: names, defaults, params, nullable annotations.

for a senior

Spot accidental duplicates in review, such as a dynamic overload next to object, and steer a team toward distinct names or a generic method when results differ.

for a principal

Treat overload sets as public API: prefer names that say what differs when only the result type changes, and keep that rule in the team's design guidelines.

## Two methods, one name A method is **overloaded** when a type declares several methods with the same name. A parser for sensor readings might want one `Parse` for `int` readings and one for `long` readings, and the first attempt often looks like this: ```csharp public int Parse(string text) => int.Parse(text); public long Parse(string text) => long.Parse(text); // error CS0111 ``` The compiler rejects the second declaration with **CS0111**: "Type 'ReadingParser' already defines a member called 'Parse' with the same parameter types". The message says *parameter types* on purpose: the return type is not what C# compares. ## What the signature contains The C# **signature** of a method is what must differ between two overloads. It is made of: - the method **name**; - the **number of type parameters**, so `Parse(string)` and `Parse<T>(string)` can coexist; - the **type of each parameter**, in order; - whether each parameter is passed **by value or by reference** (`ref`, `in`, `out`). A call such as `parser.Parse("42");` names only the method and the arguments, and it may discard the result, so the compiler has nothing to choose a return type with. Overload resolution therefore works from the arguments alone, and a signature that the arguments cannot tell apart is a duplicate. ## What is not part of it Several differences look like new overloads and are not. Each pair below was compiled with the .NET 10 SDK: | Second declaration next to the first | Result | |---|---| | `int Parse(string)` and `long Parse(string)` | CS0111: only the return type differs | | `void Log(string message)` and `void Log(string text)` | CS0111: only the parameter name differs | | `void Log(string s, int level = 0)` and `void Log(string s, int level = 1)` | CS0111: only the default value differs | | `void Add(object[] items)` and `void Add(params object[] items)` | CS0111: `params` is not part of the signature | | `void Log(string s)` and `void Log(string? s)` | CS0111: a nullable annotation is not a new type | | `void Log(object o)` and `void Log(dynamic o)` | CS0111: `dynamic` is `object` to the runtime | | `void Read(ref int v)` and `void Read(out int v)` | CS0663: differs only on parameter modifiers | The last row has its own diagnostic, CS0663: "'P' cannot define an overloaded method that differs only on parameter modifiers 'out' and 'ref'". The by-reference parameter lesson covers what `ref`, `in` and `out` mean; for overloading, what matters is that a by-value parameter and a by-reference one are distinct, while the three by-reference modifiers are not distinct from each other. ## Legal ways to get two results from one input When a type really needs both an `int` and a `long` parse, the API has a few options: 1. **Use different names**, as the base class library's `Convert` class does with `Convert.ToInt32` and `Convert.ToInt64`. 2. **Add a type parameter**: `T Parse<T>(string text) where T : IParsable<T>` lets the caller write `Parse<long>("42")`, and it can sit next to the non-generic `Parse(string)` because the number of type parameters differs. 3. **Change the parameter list**: `Parse(string text, int radix)` is a valid overload because it takes a second argument. The runnable example shows all three legal forms together, with the colliding declaration left as a comment. ## Why this matters in a review - An overload is chosen when the **caller** is compiled, from the arguments the caller passes. Anything the call site cannot express, such as the return type it wants, cannot drive that choice. - A nullable annotation added to an existing parameter never creates a second method; it only changes the warnings callers see. - Overloads that differ only in a default value are a sign that an optional parameter was meant; the lesson on optional and named arguments covers that design. - A legal overload set is only the first check. Whether a given call reaches the overload its author intended is a separate question, answered by overload resolution: the rules that rank candidates by how well each argument converts, and the tie-breakers that run when they match equally well. - The same signature rules apply to constructors and indexers, so two constructors that differ only by a parameter name or a default value collide in the same way. A junior candidate who answers "you can't overload on return type, because the compiler picks the method from the arguments" has the core of it; listing what else is left out of the signature shows they have hit these errors in real code.

  • In C#, can `void Save(int id)` and `void Save<T>(int id)` live in the same class, and how does a caller reach each one?
    Yes. The number of type parameters is part of the signature, so the two do not collide. `Save(5)` binds to the non-generic method, because `T` appears in no parameter and cannot be inferred, which leaves the generic method inapplicable. `Save<string>(5)` names the type argument and reaches the generic one.
  • Why does C# accept `Read(int value)` next to `Read(ref int value)` but reject `Read(ref int value)` next to `Read(out int value)`?
    Passing by value and passing by reference make different signatures, so the first pair is a legal overload and the call site picks one by writing `ref` or not. `ref`, `in` and `out` are all by-reference, and methods that differ only between them are error CS0663, "cannot define an overloaded method that differs only on parameter modifiers".

  • Two methods can be overloads when only their return types differ.
  • Renaming a parameter is enough to declare a second overload.
  • A different default value on an optional parameter makes a new signature.
  • string and string? are separate parameter types for overloading.
  • Adding params to an array parameter creates a distinct overload.