In C#, a logging facade has `Log(object)`, `Log(string)`, `Log<T>(T)` and `Log(ReadOnlySpan<char>)`; why does `Log(42)` reach the generic overload, and a `char[]` miss the span one?
answer
- the generic overload infers T exactly
- identity beats boxing and other conversions
- non-generic wins only a tie
- T cannot be a ref struct
basics
~20 sLog<T>(T) infers T from the argument, so its parameter is an identity match, which beats boxing 42 to object and converting a char[] to ReadOnlySpan<char>. Non-generic overloads win only ties, which is why strings still reach Log(string).
solid answer
~50 sFor a generic candidate, the compiler first infers `T` from the argument, then compares the **constructed** method like any other. `Log(42)` becomes `Log<int>(int)`, an **identity** conversion, while `Log(object)` needs boxing; identity is better, so the generic overload wins. A `char[]` gives `Log<char[]>(char[])`, again identity, which beats the implicit conversion to `ReadOnlySpan<char>`, so the span overload is missed. The rule "a non-generic method beats a generic one" is only a **tie-breaker** for when the parameter types are identical: `Log("ready")` reaches `Log(string)` and an `object` variable reaches `Log(object)` because both candidates match by identity. A `Span<char>` reaches the span overload because `T` cannot be a ref struct without `allows ref struct`, so the generic candidate is not applicable. To route arrays to the span overload, callers write `AsSpan()`, or the author changes the set, for example with C# 13's `OverloadResolutionPriorityAttribute`.
code
csharp · 17 linesvar log = new Logger();
log.Log(42); // Log<Int32>: identity beats boxing to object
log.Log("ready"); // Log(string): identity on both, non-generic wins the tie
object boxed = "ready";
log.Log(boxed); // Log(object): identity on both, non-generic wins the tie
char[] buffer = ['o', 'k'];
log.Log(buffer); // Log<Char[]>: identity beats the conversion to ReadOnlySpan<char>
log.Log(buffer.AsSpan()); // Log(ReadOnlySpan<char>): T cannot be the ref struct Span<char>
log.Log(null); // Log(string): T cannot be inferred from a null literal
class Logger
{
public void Log(object? value) => Console.WriteLine("Log(object)");
public void Log(string? message) => Console.WriteLine("Log(string)");
public void Log<T>(T value) => Console.WriteLine($"Log<{typeof(T).Name}>");
public void Log(ReadOnlySpan<char> text) => Console.WriteLine("Log(ReadOnlySpan<char>)");
}go deeper
Recall that a generic method's type parameter is inferred from the argument, so it often matches the argument's type exactly.
Explain that after inference the generic candidate competes on conversions, and that non-generic preference is only a tie-breaker.
Diagnose why a new overload is never reached when a generic catch-all exists, and pick a fix: call-site conversion, narrower overloads or explicit priority.
Decide whether a shared facade should carry a generic catch-all at all, since every overload added later must compete with its identity matches.
## A facade that grew a generic overload Logging facades often start with `Log(object)` and `Log(string)`. Someone then adds `Log<T>(T value)` so that value types such as `int` or a `TimeSpan` are not boxed, and later a teammate adds `Log(ReadOnlySpan<char> text)` so that callers can log slices of a buffer without allocating a string: ```csharp public void Log(object? value) { } public void Log(string? message) { } public void Log<T>(T value) { } public void Log(ReadOnlySpan<char> text) { } ``` Then a reviewer notices that the span overload is almost never called. The reason is how C# treats a generic candidate. ## Why the generic overload was there `Log(object)` accepts anything, but a value-type argument such as `42` has to be **boxed**: the compiler copies it into a new heap object so that it can be passed as an `object`. A logging call on a hot path can therefore allocate on every call. `Log<T>(T value)` avoids that at the call site, because the argument is passed as a `T`, and for `Log(42)` that is an `int`. The boxing lesson covers what boxing costs and where else it hides; the point here is the consequence for overload resolution. The generic method was meant as a value-type helper, but nothing in its declaration limits it to value types, so it takes part in almost every call with one argument. ## Inference first, then an ordinary comparison For a generic method, the compiler runs **type inference** on the arguments, substitutes the inferred type arguments, and then treats the result as a normal candidate with concrete parameter types: 1. `Log(42)` infers `T = int`, giving `Log<int>(int)`. 2. That candidate's parameter is exactly the argument's type: an **identity conversion**. 3. The non-generic `Log(object)` needs a **boxing conversion** for the same argument. 4. An identity conversion is better than any other conversion, so `Log<int>` is the better function member. So the generic overload wins **because of a better conversion**, not despite being generic. The same thing happens to a `char[]`: inference gives `Log<char[]>(char[])`, an identity match, while reaching `Log(ReadOnlySpan<char>)` needs a conversion (C# 14 calls it an implicit span conversion). The generic overload absorbs arrays, `int` values and user types that have no overload of their own. ## Non-generic wins only a tie The rule "a non-generic method is better than a generic method" is a **tie-breaker**. The compiler applies it only when the two candidates' parameter types are identical for the given arguments: | Call | Candidates that tie | Bound to | |---|---|---| | `Log("ready")` | `Log(string)` and `Log<string>(string)` | `Log(string)` | | `Log(boxed)` with `object boxed` | `Log(object)` and `Log<object>(object)` | `Log(object)` | | `Log(42)` | no tie: identity vs boxing | `Log<Int32>` | | `Log(buffer)` with `char[] buffer` | no tie: identity vs span conversion | `Log<Char[]>` | This is why the facade still behaves well for strings and pre-typed objects while catching everything else in the generic method. ## Where the generic candidate drops out A generic candidate can be **not applicable** at all, and then the other overloads compete without it: - **Ref structs.** `Span<char>` is a ref struct. Since C# 13 a type parameter can accept one only when it declares `allows ref struct`; `Log<T>` does not, so `Log(buffer.AsSpan())` cannot use it and binds to `Log(ReadOnlySpan<char>)`. - **A null literal.** Inference has nothing to work with for `Log(null)`, so `Log<T>` is not applicable and the call lands on `Log(string)`. ## Getting callers to the span overload Once the cause is clear, the options are: - **Call site:** `Log(buffer.AsSpan())` gives the argument a `Span<char>` type, which only the span overload accepts. - **Remove or narrow the generic overload:** if boxing avoidance is only needed for a few value types, specific overloads such as `Log(int)` do not swallow arrays. - **Rank the overloads explicitly:** C# 13 (with the attribute in the .NET 9 libraries) added `[OverloadResolutionPriority]`, which removes lower-priority applicable overloads declared in the same type. Marking `Log(ReadOnlySpan<char>)` with priority 1 sends strings and arrays to it, at the cost of making `Log(string)` hard to reach from source. Every one of these is decided when the caller compiles, so callers built before the change keep their old binding until they are rebuilt.
- Why does `Log("ready")` still reach `Log(string)` when `Log<T>(T)` would also match it by identity?Both candidates have the parameter type `string` once `T` is inferred, so the conversions tie. The compiler then applies the tie-breakers, and the first one says a non-generic method is better than a generic one. That rule only runs on such a tie, which is why `Log(42)` still goes to the generic overload.
- Why does `Log(buffer.AsSpan())` reach `Log(ReadOnlySpan<char>)` while `Log(buffer)` does not?`AsSpan()` returns a `Span<char>`, a ref struct. `Log<T>` declares no `allows ref struct` (a C# 13 anti-constraint), so `T` cannot be `Span<char>` and the generic candidate is not applicable. Among the rest, only the span overload accepts the argument. A plain `char[]` keeps the generic candidate alive, and its identity match wins.
- A non-generic overload always beats a generic one.
- Log(42) goes to Log(object) because int is an object.
- The compiler prefers ReadOnlySpan<char> because it avoids allocation.
- A generic overload is a fallback that only runs when nothing else matches.
Assumes C# 14 on .NET 10; identity beating boxing is long-standing, while allows ref struct and OverloadResolutionPriorityAttribute arrived in C# 13 and the implicit span conversion in C# 14.