skip to content

In C#, a class implements IPaymentGateway and IAuditLog, both declaring void Close(); what does one public Close do, and how do you split them?

level: middleimportance: should knowfreq 45%

answer

  1. mapping by name and signature
  2. one method, two interface slots
  3. explicit for the odd one out
  4. property and method sharing a name

basics

~20 s

One public void Close() implements both interface members, so either interface reference runs the same body. To give IAuditLog its own behaviour, implement void IAuditLog.Close() explicitly; the public Close then serves IPaymentGateway and class-typed callers.

solid answer

~40 s

C# maps each interface member to an implementation by name and signature, so a single `public void Close()` satisfies both `IPaymentGateway.Close` and `IAuditLog.Close`: one body, called through either interface. If closing the gateway connection and flushing the audit file are different jobs, implement one or both explicitly. With `public void Close()` plus `void IAuditLog.Close()`, an `IAuditLog` reference runs the explicit body and everything else runs the public one. If both are explicit, the class has no `Close` of its own and class-typed callers get CS1061. Explicit implementation is required when the two members clash by kind: a `Status` property in one interface and a `Status()` method in the other cannot both be public members, which is CS0102.

code

csharp · 23 lines
csharp
#:property PublishAot=false
var shared = new SharedCloseAdapter();
((IPaymentGateway)shared).Close();
((IAuditLog)shared).Close();

var split = new SplitCloseAdapter();
((IPaymentGateway)split).Close();
((IAuditLog)split).Close();
split.Close();

public interface IPaymentGateway { void Close(); }
public interface IAuditLog { void Close(); }

public class SharedCloseAdapter : IPaymentGateway, IAuditLog
{
    public void Close() => Console.WriteLine("one Close for both interfaces");
}

public class SplitCloseAdapter : IPaymentGateway, IAuditLog
{
    public void Close() => Console.WriteLine("public Close: releases the gateway connection");
    void IAuditLog.Close() => Console.WriteLine("IAuditLog.Close: flushes the audit file");
}

go deeper

for a junior

Remember that one public method can implement same-named members of two interfaces, and that an explicit implementation gives one interface its own body.

for a middle

Explain interface mapping: per interface, explicit implementations first, then public members by name and signature, and why CS0102 forces explicit implementation for a property and a method sharing a name.

for a senior

Check which static type each caller holds, and test through each interface, because removing an explicit member silently re-routes calls to the public one.

for a principal

When two contracts collide on a name in a shared adapter, decide whether the adapter should be split into two classes rather than carry explicit implementations for both.

## How C# matches interface members For every interface a class implements, the compiler builds an **interface mapping**: for each interface member, which class member implements it. The search starts at the class and walks up through its base classes, and at each level it checks two things in order: 1. An **explicit implementation** whose qualified name matches the member, such as `void IAuditLog.Close()`. 2. Otherwise, a **public instance member** with the same name and signature. The search runs separately for each interface. Nothing stops one public method from being found twice, which is exactly what happens when two interfaces declare the same member. ## One public method serves both ```csharp public interface IPaymentGateway { void Close(); } public interface IAuditLog { void Close(); } public class SharedCloseAdapter : IPaymentGateway, IAuditLog { public void Close() { /* one body */ } } ``` There is no error and no ambiguity. Both interface members map to the same method, so `((IPaymentGateway)a).Close()` and `((IAuditLog)a).Close()` run the same body once each. That is right when "close" means the same thing to both callers, for example releasing a single connection. ## Splitting the behaviour When the jobs differ, such as releasing the gateway connection and flushing the audit file, an explicit implementation takes one of the members out of the shared mapping: | Declarations in the class | Through `IPaymentGateway` | Through `IAuditLog` | Through the class type | |---|---|---|---| | `public void Close()` | public `Close` | public `Close` | public `Close` | | `public void Close()` + `void IAuditLog.Close()` | public `Close` | explicit body | public `Close` | | `void IPaymentGateway.Close()` + `void IAuditLog.Close()` | explicit body | explicit body | CS1061, no `Close` | The middle row is the common choice: the class keeps a natural `Close()` for its own callers, and the audit log gets its own body. ## When explicit implementation is mandatory Sometimes the two members cannot share one declaration at all: - **Different member kinds with one name.** `IHasStatus` declares `string Status { get; }` and `IStatusCheck` declares `string Status()`. A class cannot declare both a public property and a public method named `Status`; the compiler reports CS0102, "The type already contains a definition for 'Status'". At least one must be explicit. - **Same name, different return type.** `void Close()` in one interface and `bool Close()` in another cannot both be public methods, because C# methods cannot overload on return type. One must be explicit; a single public method with the wrong return type does not implement the other (CS0738). ## What explicit implementation does not change - Each interface still has exactly one implementation per member; a call never runs both bodies. - The base-list order (`IPaymentGateway, IAuditLog` or the reverse) has no effect on which body runs. - Explicit members cannot be `virtual`, so if subclasses need to change the audit flush, the explicit member should call a `protected virtual` method. ## Choosing which `Close` stays public When only one of the two members can keep the plain name, decide by who calls the class directly: 1. Keep public the meaning that **class-typed callers** expect. For a payment adapter, `adapter.Close()` in application code almost certainly means "release the gateway connection". 2. Make the more specialised contract explicit, here `void IAuditLog.Close()`, so only code that already holds an `IAuditLog` can trigger the audit flush. 3. If the two jobs share steps, put them in a private helper that both bodies call, rather than having one body call the other through a cast. 4. If the two contracts keep growing apart, that is a sign the adapter is doing two jobs; two classes, one per interface, remove the collision entirely. A unit test can pin the decision: `typeof(SplitCloseAdapter).GetInterfaceMap(typeof(IAuditLog))` lists which method implements each interface member, so a test fails if the mapping drifts. ## Reviewing such an adapter In a pull request, check what each caller holds. Code that disposes "everything closable" through one interface will run whichever body that interface maps to, and a later refactor that deletes the explicit `IAuditLog.Close` silently re-routes audit callers to the public method without any compiler warning, because the public method still matches.

  • What if one interface declares Status as a property and the other as a method?
    A class cannot have a public property and a public method both named `Status`, so declaring both is CS0102. Implement at least one explicitly, for example `string IStatusCheck.Status() => ...` next to a public `Status` property; each interface then maps to its own member.
  • Does deleting the explicit IAuditLog.Close produce a compiler error?
    No. The public `Close()` still matches `IAuditLog.Close` by name and signature, so the mapping silently falls back to it and audit callers start running the gateway's close logic. Only a test that calls through `IAuditLog` catches the change.

  • The compiler rejects a class whose two interfaces declare the same method.
  • One public Close runs twice, once for each interface.
  • The interface listed first in the base list wins the shared method.
  • Interface members match by name only, so bool Close() satisfies void Close().
  • You need two public Close methods with different modifiers to serve both.