In C#, GatewaySession implements IGatewaySession.Close with a non-virtual public method; why does a subclass's new Close not run through an IGatewaySession reference?
answer
- the mapping belongs to the declaring class
- new hides for class-typed callers only
- re-list the interface
- virtual base method instead
basics
~20 sGatewaySession's interface mapping sends Close to GatewaySession.Close, and subclasses inherit that mapping. A new method hides only for class-typed callers; re-listing the interface on the subclass, or a virtual base method, changes what interface callers run.
solid answer
~40 sFor each class that lists an interface, the compiler records which member implements each interface member. `GatewaySession : IGatewaySession` maps `Close` to its non-virtual `public void Close()`, and `HiddenClose : GatewaySession` inherits that mapping, so `public new void Close()` in the subclass is reached only by callers whose static type is `HiddenClose`. Two fixes change interface dispatch: make the base method `virtual` and `override` it, or **re-implement** the interface by listing it again (`class ReimplementedClose : GatewaySession, IGatewaySession`), which makes the compiler build a fresh mapping that picks the subclass's `Close`. The same mapping rules let an inherited base-class method implement an interface the base never listed, as in `BorrowedClose : ConnectionBase, IGatewaySession`.
code
csharp · 32 lines#:property PublishAot=false
IGatewaySession hidden = new HiddenClose();
hidden.Close();
IGatewaySession reimplemented = new ReimplementedClose();
reimplemented.Close();
IGatewaySession borrowed = new BorrowedClose();
borrowed.Close();
new HiddenClose().Close(); // class-typed call sees the new method
public interface IGatewaySession { void Close(); }
public class GatewaySession : IGatewaySession
{
public void Close() => Console.WriteLine("GatewaySession.Close"); // non-virtual
}
public class HiddenClose : GatewaySession
{
public new void Close() => Console.WriteLine("HiddenClose.Close");
}
public class ReimplementedClose : GatewaySession, IGatewaySession // re-lists the interface
{
public new void Close() => Console.WriteLine("ReimplementedClose.Close");
}
public class ConnectionBase
{
public void Close() => Console.WriteLine("ConnectionBase.Close");
}
public class BorrowedClose : ConnectionBase, IGatewaySession { } // base method implements itgo deeper
Recall that a subclass's new method is not seen by callers holding the interface or the base type.
Explain interface mapping: built per class that lists the interface, inherited by subclasses, searched up through base classes.
Diagnose an override that never runs through an interface, and choose between a virtual base method and interface re-implementation when the base is not yours.
Set the rule for library base classes: if subclasses are meant to change interface behaviour, make the implementing members virtual rather than leaving teams to re-implement.
## Interface mapping is per declaring class When a C# class lists an interface in its base list, the compiler builds that class's **interface mapping**: for each interface member, the class member that implements it. It searches the class first, then its base classes, taking explicit implementations before public members with a matching name and signature at each level. The runtime dispatches every call through the interface according to that mapping. A derived class that does **not** list the interface again simply inherits its base class's mapping. ## Why `new` does not reach interface callers ```csharp public class GatewaySession : IGatewaySession { public void Close() => Console.WriteLine("GatewaySession.Close"); // non-virtual } public class HiddenClose : GatewaySession { public new void Close() => Console.WriteLine("HiddenClose.Close"); } ``` `IGatewaySession.Close` was mapped to `GatewaySession.Close` when `GatewaySession` was compiled. `HiddenClose` inherits the mapping, and its `new` method is a different member that merely shares the name. So: - `((IGatewaySession)new HiddenClose()).Close()` prints `GatewaySession.Close`; - `new HiddenClose().Close()` prints `HiddenClose.Close`, because the compiler binds a class-typed call to the closest member with that name. This is the same split that `new` causes for a base-typed variable, now applied to an interface reference. ## Three ways to change what interface callers run 1. **Virtual base method.** If `GatewaySession.Close` is `virtual` and the subclass uses `override`, the mapping points at a virtual slot and the call dispatches to the override. This needs control of the base class. 2. **Interface re-implementation.** Listing the interface again, `class ReimplementedClose : GatewaySession, IGatewaySession`, makes the compiler build a new mapping for the subclass, which finds the subclass's `public new void Close()` first. This works even when the base class is in a package you cannot change. 3. **Explicit re-implementation.** With the interface re-listed, the subclass may instead declare `void IGatewaySession.Close()`; that also replaces the base class's explicit implementation, which a subclass could never override. ## A base class method can implement an interface it never listed The mapping search walks up the hierarchy, so an inherited public method qualifies: ```csharp public class ConnectionBase { public void Close() { /* ... */ } } public class BorrowedClose : ConnectionBase, IGatewaySession { } ``` `BorrowedClose` compiles with no members of its own, and `IGatewaySession.Close` runs `ConnectionBase.Close`. This is useful when adapting an existing class to a new interface, and surprising when a method that happened to have a matching name starts acting as an implementation. ## Interface inheritance and explicit names When `IRefundableGateway : IPaymentGateway`, a class implementing `IRefundableGateway` must implement both interfaces' members, because it implements the base interface too. An explicit implementation must name the interface that **declares** the member: `void IPaymentGateway.Charge(decimal)` works, while `void IRefundableGateway.Charge(decimal)` is CS0539, "in explicit interface declaration is not found among members of the interface that can be implemented". ## Seeing the mapping The mapping is not hidden. `Type.GetInterfaceMap(typeof(IGatewaySession))` returns, for each interface method, the method the runtime will call on that type: - for `HiddenClose` it reports `GatewaySession.Close`, the inherited mapping; - for `ReimplementedClose` it reports `ReimplementedClose.Close`, the fresh mapping; - for `BorrowedClose` it reports `ConnectionBase.Close`. The compiler also helps when it can. A non-virtual public method that implements an interface member is emitted as `virtual` and `final` (sealed) in metadata, which gives the runtime a slot to dispatch through while still preventing subclasses from overriding it. A subclass that writes `override` on it gets CS0506, because at the language level the method is not virtual. That error is often the first clue that the base author never intended the behaviour to change. ## Summary for a code review | Situation | Interface call runs | |---|---| | Subclass declares `new Close`, does not re-list | base class method | | Base `virtual Close`, subclass `override` | subclass override | | Subclass re-lists the interface and declares `Close` | subclass method | | Base class has a matching public method, subclass lists the interface | base class method | When a subclass's change "does nothing" for code that holds the interface, check which class lists the interface and whether the base method is virtual before suspecting anything else.
- When would you re-list the interface instead of making the base method virtual?When the base class lives in a package you cannot change, or its author deliberately kept the method non-virtual. Re-listing remaps the interface only for the subclass and its descendants; code inside the base class that calls through the interface on a subclass instance also gets the subclass's method, which can surprise the base class's author.
- Why is void IRefundableGateway.Charge(decimal) rejected when Charge is declared on IPaymentGateway?An explicit implementation must be qualified with the interface that declares the member. `IRefundableGateway` only inherits `Charge`, so the compiler reports CS0539, and because `IPaymentGateway.Charge` is then unimplemented it also reports CS0535. Writing `void IPaymentGateway.Charge(decimal amount)` fixes both.
- A new method in a subclass replaces the interface implementation for all callers.
- A base class method can only implement interfaces the base class itself lists.
- Listing an interface again on a subclass that already inherits it is a compile error.
- An explicit implementation may be qualified with a derived interface's name.
- A subclass can override its base class's explicit interface implementation.