Your shared C# package publishes IPaymentGateway, implemented by three teams; how do you add a Refund operation: a default interface method, a new interface, or an abstract base class?
answer
- binary vs source compatibility
- TypeLoadException for old implementers
- who must know refunds exist
- runtime and base-class constraints
basics
~20 sAdding an abstract member breaks implementers: CS0535 on recompile, TypeLoadException for old builds. A default method fits when a real default exists, a derived IRefundableGateway fits an optional capability, and an abstract base class costs each team its base-class slot.
solid answer
~50 sAdding an abstract `Refund` to `IPaymentGateway` breaks every implementer: recompiling gives CS0535, and an adapter assembly built against the old version fails to load with `TypeLoadException` ("Method 'Refund' ... does not have an implementation"). A **default interface method** is binary and source compatible, but it only fits when the default is honest; a body that throws turns a build break into a run-time failure, the method is invisible on the concrete types, and every consumer needs a runtime with default-implementation support. A **new interface** `IRefundableGateway : IPaymentGateway` makes refunds an opt-in capability that callers test with `is`, at the cost of type checks and one more contract. An **abstract base class** can grow non-abstract members safely and hold shared state, but every adapter must give up its own base class and structs are excluded. The deciding questions: is there an honest default, and must callers know who can refund?
code
csharp · 30 lines#:property PublishAot=false
IPaymentGateway[] gateways = [new LegacyCardAdapter(), new ModernCardAdapter()];
foreach (var gateway in gateways)
{
if (gateway is IRefundableGateway refundable)
refundable.Refund("tx-7", 12m);
else
Console.WriteLine($"{gateway.GetType().Name}: refunds not supported, open a manual ticket");
}
public interface IPaymentGateway // v1, unchanged
{
void Charge(decimal amount);
}
public interface IRefundableGateway : IPaymentGateway // v2 capability, opt-in
{
void Refund(string transactionId, decimal amount);
}
public class LegacyCardAdapter : IPaymentGateway
{
public void Charge(decimal amount) { }
}
public class ModernCardAdapter : IRefundableGateway
{
public void Charge(decimal amount) { }
public void Refund(string transactionId, decimal amount) => Console.WriteLine($"ModernCardAdapter: refunded {transactionId}");
}go deeper
Know that adding an abstract member to an interface breaks every class that implements it, and that default methods were added to C# to avoid that.
Explain the difference between source compatibility (CS0535 on recompile) and binary compatibility (TypeLoadException for old assemblies).
Show the hidden costs of a default method: invisible on concrete types, a dishonest default, the runtime requirement and future diamonds.
Own the decision and the policy: whether a capability is universal or optional, which target frameworks the package serves, and when a breaking major version is cheaper.
## The constraint: three teams you do not recompile A published interface is a contract compiled into other teams' assemblies. Each team's adapter was built against version 1 of `IPaymentGateway`; they upgrade the package on their own schedule, and a host may load a new package version next to an adapter assembly that was never rebuilt. Any way of adding `Refund` has to be judged at two moments: when a team **recompiles** against version 2 (source compatibility), and when an old adapter assembly **runs** against version 2 (binary compatibility). ## Option 0: add an abstract member (the one to rule out) Adding `void Refund(string transactionId, decimal amount);` without a body breaks both: - recompiling an adapter gives CS0535, "'TeamCardAdapter' does not implement interface member 'IPaymentGateway.Refund(string, decimal)'"; - an adapter assembly that is not recompiled fails when the runtime loads its type: `System.TypeLoadException: Method 'Refund' in type 'TeamCardAdapter' ... does not have an implementation.` That is a major-version change, and it fails in production rather than at a caller. ## Option 1: a default interface method `void Refund(string transactionId, decimal amount) => ...` with a body keeps old adapters loading and recompiling. Its costs: - **The default must be meaningful.** The body can use only the interface's own members and static data, because interfaces hold no instance state. A default that throws `NotSupportedException` keeps builds green and moves the failure to the first refund request. - **It is invisible on concrete types.** Classes do not inherit default methods, so team code that holds `TeamCardAdapter` cannot call `Refund` without converting to the interface. - **Runtime requirement.** Default implementations need .NET Core 3.0 or later; a package's .NET Framework build cannot declare them (CS8701). - **Diamonds later.** If two derived interfaces each override `Refund`, a class implementing both must resolve it (CS8705). ## Option 2: a new interface `public interface IRefundableGateway : IPaymentGateway { void Refund(...); }` leaves version 1 untouched. Teams that support refunds implement the new interface; callers ask `if (gateway is IRefundableGateway refundable)`. This states the truth in the type system: some gateways cannot refund, and the caller must decide what to do about it. The costs are type tests at call sites and one more interface each time a capability is added. ## Option 3: an abstract base class A `PaymentGatewayBase` can gain non-abstract `virtual` members in later versions without breaking subclasses (a subclass that already has a same-named method only gets warning CS0114), and it can hold shared state and validate in a constructor. But adopting it means every team's adapter must derive from it, giving up any base class it already has, and structs cannot use it. Switching from an interface to a base class is itself a breaking change. ## Comparing the options | | Default method | New interface | Abstract base class | |---|---|---|---| | Old adapter binaries keep loading | yes | yes | n/a, requires migration | | Callers learn which gateways refund | no, default runs | yes, `is` test | no, base virtual runs | | Needs a meaningful default | yes | no | yes | | Constraint on implementers | modern runtime | none | single base-class slot | ## A decision procedure 1. If the package must support runtimes without default-implementation support, choose between the new interface and a later major version. 2. If a correct default exists for every gateway (for example, delegating to existing `Charge` with a negative amount is **not** one), a default method is the lowest-friction path. 3. If refund support genuinely varies and callers must react, publish `IRefundableGateway`. 4. Reserve an abstract base class for a new family of types where shared state and construction rules matter more than each team's freedom to pick a base class. ## Rolling the change out - Ship the default method or the new interface in a minor version, and say in the release notes which implementers should override or adopt it. - Ask each team for a test that calls `Refund` through `IPaymentGateway`, or checks `is IRefundableGateway`, so every adapter's behaviour is explicit rather than inherited by accident. - If an abstract member is ever truly needed, ship it in a major version and state that adapter binaries built against the old version will not load. - Mark members you plan to remove with `[Obsolete]` a version ahead, so implementers and callers see warnings before the break. Whatever is chosen, record it in the package's versioning policy, so the next added operation does not reopen the debate.
- Is a default Refund that throws NotSupportedException a good compromise?It keeps every adapter compiling and loading, but it hides the gap until the first refund in production and gives callers no way to check support beforehand. If refund support varies by gateway, a capability interface tells callers the truth at the type level; reserve the throwing default for operations where an exception is a reasonable answer.
- What happens to a team's adapter assembly, built against version 1, when version 2 adds an abstract interface member and only the package is upgraded?The runtime refuses to load the adapter type: `TypeLoadException`, "Method 'Refund' in type 'TeamCardAdapter' ... does not have an implementation." The failure appears when the type is first loaded, not when `Refund` is called, so even code paths that never refund stop working.
- Adding a member to a published interface is safe once implementers recompile.
- A default interface method adds Refund to every implementing class's own surface.
- An abstract base class lets each team keep the base class it already has.
- A default method that throws is as safe as a compile-time check.
- Default interface methods work for consumers on any target framework.