In C#, why does a `short` argument bind to `Write(int)` over `Write(long)` and `Write(object)`, and why can a literal `0` pick an enum overload over `Write(object)`?
answer
- rank the parameter types, not the argument
- int converts to long, not back
- boxing is the weakest match
- constant conversions for literals
basics
~20 sC# ranks the parameter types: int converts implicitly to long but not back, so int is the better target, and boxing to object loses to both. A literal 0 converts implicitly to any enum, which converts to object, so the enum overload wins.
solid answer
~40 sA `short` converts implicitly to `int`, `long` and `object`, so all three overloads are applicable. The compiler then compares the targets: one type is a **better conversion target** than another when an implicit conversion runs from it to the other and not back. `int` converts to `long`, but `long` does not convert to `int`, so `Write(int)` beats `Write(long)`; both numeric types convert to `object`, so `Write(object)`, a boxing conversion, loses to both. Literals add **constant conversions**: the constant `0` converts implicitly to any enum type, so with `Set(object)` and `Set(LogLevel)`, `Set(0)` binds to the enum overload, while `Set(1)` and an `int` variable holding 0 go to `Set(object)`. In the same way, a constant `5` fits `uint`, so `Find(5)` picks `Find(uint)` over `Find(long)`, while an `int` variable picks `Find(long)`.
code
csharp · 31 linesvar log = new LogWriter();
short retries = 3;
log.Write(retries); // Write(int)
log.Write(5L); // Write(long)
var ids = new IdIndex();
ids.Find(5); // Find(uint): the constant 5 fits in uint, and uint converts to long
int id = 5;
ids.Find(id); // Find(long): an int variable has no implicit conversion to uint
var filter = new LevelFilter();
filter.Set(0); // Set(LogLevel): the literal 0 converts to any enum type
filter.Set(1); // Set(object): only a constant zero converts to an enum
class LogWriter
{
public void Write(int value) => Console.WriteLine("Write(int)");
public void Write(long value) => Console.WriteLine("Write(long)");
public void Write(object value) => Console.WriteLine("Write(object)");
}
class IdIndex
{
public void Find(uint id) => Console.WriteLine("Find(uint)");
public void Find(long id) => Console.WriteLine("Find(long)");
}
enum LogLevel { Trace, Info, Warn }
class LevelFilter
{
public void Set(object value) => Console.WriteLine("Set(object)");
public void Set(LogLevel level) => Console.WriteLine("Set(LogLevel)");
}go deeper
Recall that an exact type match wins, that a narrower number widens to the closest numeric overload, and that object is the last resort.
Explain the better conversion target rule: the parameter type that converts implicitly to the other, and not back, is the better match.
Diagnose bindings that change between a literal and a variable, such as zero reaching an enum overload, and fix call sites with typed arguments.
Keep an object or params object[] overload away from enum and numeric overloads in a shared API, where literals can land on the wrong one.
## The argument does not have to match exactly When an argument's type is not one of the parameter types, C# still accepts the call if an **implicit conversion** exists, and then has to choose among every overload that is applicable. A logging facade with these overloads shows the common case: ```csharp public void Write(int value) { } public void Write(long value) { } public void Write(object value) { } ``` A `short` variable converts implicitly to all three parameter types: widening to `int`, widening to `long`, and **boxing** to `object`. All three are applicable, so the compiler ranks them. ## Better conversion target For each argument, C# compares the two conversions. When neither is an identity conversion, the decision usually falls to the **better conversion target** rule: of two different types, `T1` is the better target when an implicit conversion from `T1` to `T2` exists and no implicit conversion from `T2` to `T1` does. The rule ranks the **parameter types against each other**, not against the argument: - `int` converts implicitly to `long`, and `long` does not convert implicitly to `int`, so `int` is better than `long`. - `int` and `long` both convert to `object`, and `object` converts to neither, so both are better than `object`. - `Write(int)` is better than both other candidates, so `Write(short)` calls `Write(int)`. A further clause in the same rule covers signed and unsigned types: when neither converts to the other, a signed integral type beats an unsigned one, for example `long` over `ulong`. ## Literals bring constant conversions An integer literal is a **constant expression**, and constants have extra implicit conversions that variables lack: 1. A constant `int` converts implicitly to `sbyte`, `byte`, `short`, `ushort`, `uint` or `ulong` when its value fits in the target type. 2. The constant **zero** converts implicitly to any **enum** type. Both can make a call bind somewhere surprising: | Overloads | Call | Bound to | Why | |---|---|---|---| | `Find(uint)`, `Find(long)` | `Find(5)` | `Find(uint)` | `5` fits in `uint`, and `uint` converts to `long` | | `Find(uint)`, `Find(long)` | `Find(id)` with `int id = 5` | `Find(long)` | a variable has no conversion to `uint` | | `Set(object)`, `Set(LogLevel)` | `Set(0)` | `Set(LogLevel)` | `0` converts to the enum, and the enum converts to `object` | | `Set(object)`, `Set(LogLevel)` | `Set(1)` | `Set(object)` | only zero converts to an enum | The enum case is the classic production surprise. An API with an `(string, object)` overload and a `(string, SomeEnum)` overload treats `0` as the enum's zero member, so passing a literal zero as a value reaches the enum overload, while passing `1` reaches the `object` one. The fix at the call site is to give the argument the intended type, such as `(object)0` or a typed variable. ## When an exact match exists If one overload's parameter type is the argument's own type, the **identity conversion** wins outright. With `Write(int)`, `Write(long)`, `Write(object)` and `Write(LogLevel)` all declared, `Write(0)` calls `Write(int)`: the literal is an `int`, and identity beats the constant conversion to the enum. The enum trap appears only when no `int` overload exists. ## Floating-point targets The same rule applies between `float` and `double`, with a sharper edge. With only `Write(float)` and `Write(double)` declared: - `Write(3)` calls `Write(float)`: an `int` converts implicitly to both, and `float` converts implicitly to `double`, so `float` is the better target. - `long big = 16_777_217; Write(big);` also calls `Write(float)`, and the value printed is `16777216`, because a `float` cannot hold every integer of that size. The more precise overload was available and lost. - With `Write(decimal)` and `Write(double)`, `Write(3)` is error CS0121: no implicit conversion exists between `decimal` and `double` in either direction, so neither target is better. ## Reviewing numeric overload sets - Read every call with a literal or a narrower argument against the full overload list, because the winner is decided by the parameter types, not by the argument's own type. - Watch for an `object` or `params object[]` overload next to an enum overload: a literal zero meant as a value lands on the enum. - An `int` variable and the literal with the same value can bind to different overloads, because only constants get the narrowing constant conversions. - When the intended overload matters, cast the argument or pass a variable of the exact type; the call then has an identity match. All of this is decided when the caller is compiled, so the call keeps its binding until that code is rebuilt.
- With `Write(int)`, `Write(long)`, `Write(object)` and `Write(LogLevel)` all declared, which does `Write(0)` call?`Write(int)`. The literal `0` is an `int`, so the conversion to `int` is an identity conversion, which is better than the constant conversion to `LogLevel`, the widening to `long` and the boxing to `object`. The zero-to-enum surprise only appears when no overload takes the argument's own type.
- Why can `Find(5)` and `Find(id)`, where `int id = 5`, bind to different overloads of `Find(uint)` and `Find(long)`?Only a constant expression gets the constant conversion to `uint` when its value fits. For `Find(5)` both overloads are applicable and `uint` is the better target because it converts to `long`. For `Find(id)` there is no implicit conversion from an `int` variable to `uint`, so only `Find(long)` is applicable.
- A short argument picks Write(long) because long is the safest widening.
- Write(object) wins because every argument converts to object.
- A literal 0 and an int variable holding 0 always bind to the same overload.
- Any integer literal converts implicitly to an enum type.
- Overload choice compares how close each parameter is to the argument's size.