When adding new overloads such as params Span<T> alongside existing params T[] methods, the compiler might choose an unexpected overload or report ambiguity errors. Library authors had no way to guide the compiler's choice without breaking existing callers.
C# 13 introduces OverloadResolutionPriorityAttribute to let library authors specify which overload should be preferred when multiple are applicable. Higher priority values are preferred.
Code
C#
class Logger
{
// New overload, preferred by the compiler
[OverloadResolutionPriority(1)]
public void Log(params ReadOnlySpan<string> messages) { }
// Original overload, still available for binary compatibility
public void Log(params string[] messages) { }
}
var logger = new Logger();
logger.Log("hello", "world"); // Calls the Span overloadC#
class Logger
{
// Adding a Span overload alongside an array overload
// would cause ambiguity errors at call sites
public void Log(params ReadOnlySpan<string> messages) { }
public void Log(params string[] messages) { }
}
var logger = new Logger();
logger.Log("hello", "world"); // Error: ambiguous callNotes
- The default priority is
0; higher values are preferred - Only affects overload resolution when multiple candidates are equally applicable
- Primarily designed for library authors evolving APIs without breaking existing callers