Back to results for “Extension Methods”
C# › Extension Methods

How do 'extension method ambiguity' conflicts arise when two different NuGet packages or namespaces define extension methods with the identical signature, and how do you resolve them?

Advanced

Answer

If two 'using' namespaces are both in scope and both define an applicable extension method with an identical signature for the same type, the compiler reports an AMBIGUOUS CALL error — resolved either by removing one of the conflicting 'using' directives, or by explicitly calling the desired method using its full static syntax (ClassName.Method(instance, ...)) to disambiguate.
// Both LibraryA.Extensions and LibraryB.Extensions define: string Truncate(this string s, int length)
using LibraryA.Extensions;
using LibraryB.Extensions;

string result = "hello".Truncate(3); // Error: ambiguous call between the two
// Fix: LibraryA.Extensions.StringExtensions.Truncate("hello", 3); // explicit disambiguation
Real-world example Resolving a real compile error after adding a second NuGet package that happens to define a conflicting extension method name.

Common follow-ups: Does C#'s extension method resolution have any built-in tie-breaking rule based on namespace 'closeness,' or is it always a hard ambiguity error?