skip to content

After a C# file imports a second namespace that also defines ToSlug(this string), the build fails with CS0121; how does extension lookup by namespace scope explain it, and how do you fix it?

level: seniorimportance: should knowfreq 35%

answer

  1. innermost scope first
  2. first scope with applicable candidates stops
  3. same scope, equal candidates
  4. static call or narrower using

basics

~20 s

Extension lookup searches namespace scopes innermost first and stops at the first with applicable candidates, so two equally good ToSlug methods imported at one level give CS0121. Fix it with a static call, an inner-scope using, or one fewer using.

solid answer

~50 s

Extension lookup walks outward through the caller's enclosing namespace declarations to the compilation unit. At each level it first takes extension methods in that namespace's own static classes, then those in namespaces imported by `using` directives at that level, and the first level with an applicable candidate wins. Overload resolution then runs only on that set. Two `ToSlug(this string)` methods brought in by `using Shared.Text;` and `using Legacy.Text;` at the top of the same file land in one set with identical signatures, so the compiler reports CS0121. A more specific receiver type, such as `string` against `object`, would win instead. Fixes: call `StringExtensions.ToSlug(title)` directly, move one `using` inside the namespace block so it is searched first, or remove the import you do not need. `using static` for one class does not help while the other namespace is still imported at the same level.

code

csharp · 19 lines
csharp
using Shared.Text;
using Legacy.Text;

Console.WriteLine("Hello World".ToSlug());

namespace Shared.Text
{
    public static class StringExtensions
    {
        public static string ToSlug(this string text) => text.ToLowerInvariant().Replace(' ', '-');
    }
}
namespace Legacy.Text
{
    public static class SlugHelpers
    {
        public static string ToSlug(this string text) => text.Replace(' ', '_');
    }
}

go deeper

for a junior

Know that two imported extension methods with the same signature cause CS0121, and that calling one as a static method always works.

for a middle

Describe the outward scope walk and why a nearer namespace's helper shadows a farther one.

for a senior

Diagnose CS0121 that appears after a package upgrade or a new global using, and choose the narrowest fix that keeps the file readable.

for a principal

Set rules for where shared extension classes live and how they are named, so adding a dependency cannot break unrelated files.

## How the compiler collects extension candidates When a call such as `"Hello World".ToSlug()` finds no applicable instance member, the C# compiler looks for extension methods. It does not search every referenced assembly; it walks the **namespace scopes** around the call: 1. Start at the **innermost namespace declaration** that encloses the call. 2. At that level, take the extension methods declared in the static classes of **that namespace itself**. 3. If none is applicable, take the extension methods from the namespaces **imported by `using` directives written at that level** (plus classes imported with `using static`). 4. If still none is applicable, move one level out and repeat, ending at the **compilation unit**, where top-of-file usings and `global using` directives (including implicit usings) live. 5. At the first level that produces applicable candidates, stop and run **overload resolution** on that set alone. The search stops at the first level with any applicable candidate, so a nearer helper shadows a farther one even if the farther one would be a better match. ## Why CS0121 appears A typical break: a file already has `using Shared.Text;`, and someone adds `using Legacy.Text;` to reach another helper. Both namespaces declare a `ToSlug(this string text)`. Both usings sit at the compilation-unit level, so both methods enter the **same candidate set**, and overload resolution cannot prefer either: same parameter types, same conversions. The SDK prints: `error CS0121: The call is ambiguous between the following methods or properties: 'Shared.Text.StringExtensions.ToSlug(string)' and 'Legacy.Text.SlugHelpers.ToSlug(string)'` Ambiguity needs **equally good** candidates. If the second helper were declared `ToSlug(this object value)`, the `string` version would simply win, because an identity conversion beats a conversion to `object`. C# 14 extension properties report the same situation as **CS9339**, "The extension resolution is ambiguous between the following members". ## Fixes and what each one does | Fix | Effect | |---|---| | `StringExtensions.ToSlug(title)` | Ordinary static call; no extension lookup, no ambiguity | | Move `using Shared.Text;` inside `namespace App.Billing { ... }` | That level is searched first, so its `ToSlug` wins for code in that block | | Remove the `using` you do not need | Only one candidate remains | | Replace one using with `using static Shared.Text.StringExtensions;` | Does not help while the other namespace is imported at the same level: still CS0121 | | A type alias such as `using Slugs = Shared.Text.StringExtensions;` | Imports no extension methods; use it for static calls like `Slugs.ToSlug(title)` | Moving a using inward is precise but easy to overlook in review; the static call is the most explicit fix when the clash is local to a few lines. ## A worked diagnosis Suppose a build breaks in `InvoiceFormatter.cs` after a dependency update, with CS0121 on `customer.Name.ToSlug()`: 1. Read the two fully qualified names in the message; they identify both static classes and therefore both namespaces. 2. Check which usings bring each namespace in: a line in the file, a `global using` elsewhere in the project, or the implicit usings. 3. If the file needs only one of the helpers, remove or narrow the other import; if it needs both in different places, call one of them in static form at those places. The fix touches only the caller; neither library has to change. ## Why this hurts after an upgrade The failure often appears with no edit to the failing file: - a package update adds an extension method to a namespace you already import; - a project enables implicit usings or adds a `global using`, which puts a new namespace at the compilation-unit level of **every** file; - a teammate adds a helper with a common name to a widely imported namespace. Each is a **source-breaking change** for callers even though no existing method changed. Already-compiled binaries are unaffected, because the call target is fixed in their IL. ## Habits that prevent it - Put extension classes in namespaces callers import on purpose, not in a namespace every file already has. - Avoid generic names for helpers on very common types such as `string` and `object`. - Do not expect `[OverloadResolutionPriority]` (C# 13) to settle a clash between two different static classes; it ranks members of one containing type, and the SDK still reports CS0121 across classes. - When a build breaks after an upgrade with CS0121 on an extension call, read the two fully qualified names in the message; they tell you which import to narrow.

  • Why is there no ambiguity if one helper is declared ToSlug(this object value)?
    Both methods are candidates, but overload resolution prefers the one whose receiver parameter is `string`: an identity conversion is better than a conversion to `object`. Ambiguity needs candidates that are equally good for every argument, including the receiver.
  • If the inner scope's ToSlug takes this int, which ToSlug does a string receiver bind to?
    The search moves outward. The inner level has a `ToSlug`, but it is not applicable to a `string`, so the compiler continues to the next level and uses the first applicable candidates it finds there, such as one imported by a file-level using.
  • Does a global using change extension lookup?
    Yes. A `global using` (implicit usings are generated as global usings) imports the namespace at the compilation-unit level of every file in the project, so its extension methods join the outermost candidate set everywhere and can create CS0121 in files nobody edited.

Finding a colleague's phone extension: you check your own team's list first, then the floor directory, and stop at the first list with someone of that name who can take the call. Two people with that name on the same list, and the operator has to ask which one you mean.

  • The using directive written first in the file wins the tie.
  • The compiler searches every referenced assembly for extension methods.
  • Importing one class with using static settles a clash with another imported namespace.
  • A type alias directive imports that class's extension methods.
  • An ambiguous extension call compiles and picks one candidate at run time.