In C#, why does `Write(null)` bind to `Write(string?)` when `Write(object?)` also exists, yet fail with CS0121 once `Write(Exception?)` is declared?
answer
- null has no type of its own
- which parameter type is more specific
- an implicit conversion in one direction only
- string and Exception: neither converts
basics
~20 sA null literal converts to every reference type, so the more specific parameter wins: string converts to object but not back. string and Exception do not convert to each other, so no overload is best and the call is error CS0121.
solid answer
~50 s`null` has no type, so every one-parameter overload whose parameter is a reference type or a nullable value type is applicable. The compiler then compares the parameter types: one is a **better conversion target** when an implicit conversion runs from it to the other and not back. `string` converts to `object`, so `Write(string?)` beats `Write(object?)`. Once `Write(Exception?)` exists, `string` and `Exception` have no conversion either way, so neither beats the other; the best member must beat every other candidate, and none does. The call is error CS0121: "The call is ambiguous between the following methods or properties: 'AlertLog.Write(string?)' and 'AlertLog.Write(System.Exception?)'". The fix is to give the argument a type: `Write((Exception?)null)`, a typed variable, or `default(string)`. Callers compiled before the new overload keep calling the method they were bound to; the error appears when they recompile.
code
csharp · 19 linesvar audit = new AuditLog();
audit.Write(null); // Write(string?): string converts to object, not the reverse
var alerts = new AlertLog();
// alerts.Write(null); // error CS0121: neither string nor Exception converts to the other
alerts.Write((Exception?)null); // a cast gives the null a type: Write(Exception?)
string? missing = null;
alerts.Write(missing); // a typed variable does the same: Write(string?)
class AuditLog
{
public void Write(object? value) => Console.WriteLine("AuditLog.Write(object?)");
public void Write(string? message) => Console.WriteLine("AuditLog.Write(string?)");
}
class AlertLog
{
public void Write(string? message) => Console.WriteLine("AlertLog.Write(string?)");
public void Write(Exception? error) => Console.WriteLine("AlertLog.Write(Exception?)");
}go deeper
Recall that a bare null fits any reference-type parameter, and that a cast such as (string?)null tells the compiler which overload you mean.
Explain the better conversion target rule: an implicit conversion in one direction only makes a type more specific, and no direction means CS0121.
Show that resolution happens when the caller compiles: a new overload breaks the next build of callers passing null, while their old binaries still run.
Weigh a new reference-type overload against every caller passing null or default, and decide whether a distinct method name avoids a source break.
## A literal with no type Most arguments carry a type: `42` is an `int`, `"disk full"` is a `string`. The **null literal** does not. It converts implicitly to any reference type and to any nullable value type such as `int?`, so it fits every such parameter. When a logging facade offers several one-parameter `Write` overloads, `Write(null)` is **applicable** to all of the reference-type ones, and overload resolution has to rank them. ## How the compiler ranks the candidates Overload resolution runs in three steps: 1. Collect the **applicable** candidates: methods whose parameters accept the arguments. 2. Compare candidates pairwise, argument by argument. For a null argument the conversion to each parameter is the same kind, so the comparison falls to the **better conversion target** rule: type `T1` is better than `T2` when an implicit conversion from `T1` to `T2` exists and none from `T2` to `T1`. 3. Pick the one candidate that is **better than all the others**. If there is no such candidate, the call is ambiguous. With `Write(object?)` and `Write(string?)`, step 2 has an answer: every `string` is an `object`, but not the reverse, so `string` is the more specific target and `Write(string?)` wins. The nullable annotations (`?`) play no part; they are not separate types. ## When nothing wins: CS0121 Add `Write(Exception?)` and compare it with `Write(string?)`. Neither `string` nor `Exception` converts to the other, so neither is a better target. Step 3 now fails: `Write(string?)` is not better than `Write(Exception?)`, and `Write(Exception?)` is not better than `Write(string?)`. Keeping `Write(object?)` in the set does not help, because `object` loses to both and can never be the best member. The compiler reports **CS0121**, an error and not a warning: | Overloads declared | `Write(null)` | |---|---| | `Write(object?)`, `Write(string?)` | binds to `Write(string?)` | | `Write(string?)`, `Write(Exception?)` | CS0121 | | `Write(object?)`, `Write(string?)`, `Write(Exception?)` | CS0121 | | `Write(string?)`, `Write(int?)` | CS0121 | The last row shows that a nullable value type takes part too: `int?` and `string` have no conversion between them. The `default` literal behaves the same way. `Write(default)` takes its type from the parameter, so it is just as ambiguous, while `default(string)` is an expression of type `string` and binds cleanly. The null-forgiving operator does not help: `Write(null!)` still has no type and still fails with CS0121. ## The same error without null CS0121 is not only about `null`. The C# documentation's own example has two overloads, `M(int a, double b)` and `M(double a, int b)`, and a call `M(1, 1)`. Comparing them argument by argument: - for the first argument, `M(int, double)` is better, because `1` is an `int` and matches by identity; - for the second argument, `M(double, int)` is better, for the same reason. A better function member must be at least as good for **every** argument and strictly better for at least one. Here each candidate wins one argument and loses the other, so neither is better and the call is CS0121. The null case is the one-argument version of the same rule: there is only one argument, and it ranks neither candidate above the other. ## Fixing the call site The cure is to give the argument a type, so that one candidate has an identity match: - cast it: `Write((Exception?)null)` or `Write((string?)null)`; - pass a typed variable: `string? missing = null; Write(missing);`; - use a typed default: `Write(default(string))`. Each of these makes the conversion to one parameter an identity conversion, which beats the conversion to any other parameter. ## Adding an overload is a source-breaking change Overload resolution is decided when the **caller** is compiled. An application built against a library that only had `Write(object?)` and `Write(string?)` stores a call to `Write(string?)` in its compiled code. If the library's next version adds `Write(Exception?)`: - the already-compiled application keeps running and still calls `Write(string?)`, because that method still exists; - the next time the application is **recompiled** against the new version, every `Write(null)` becomes CS0121, and the build breaks until those call sites are fixed. This is why reviewers ask whether a new overload with an unrelated reference-type parameter will collide with `null` or `default` arguments at existing call sites. It does not break binaries, but it can break the next build of every caller that passes a bare `null`.
- Why does `Write(default)` fail the same way, while `Write(default(string))` compiles?The `default` literal, like `null`, has no type of its own; it takes the type of the parameter it is converted to, so it is applicable to both `Write(string?)` and `Write(Exception?)` and neither is better. `default(string)` is an expression of type `string`, so the conversion to `Write(string?)` is an identity conversion and that overload wins.
- A library adds `Write(Exception?)` in a minor release. What happens to an application that calls `Write(null)`?Its compiled code keeps working: the call was bound to `Write(string?)` when the application was built, and that method is still there. The next rebuild against the new version fails with CS0121 at each `Write(null)`, so the release breaks source compatibility but not binary compatibility.
- A null argument binds to Write(object?) because null is an object.
- The compiler resolves Write(null) to the overload declared first.
- CS0121 is only a warning, and the call still picks one overload.
- Write(null!) tells the compiler which overload to use.
- Adding an overload never breaks callers that already compile.