Skip to content

Implementing Colliding Interface Members

Question

In which ways can you implement multiple interfaces that have a common method signature?

Short interview answer

One public method can implicitly implement compatible members from multiple interfaces. Use explicit interface implementation when each interface needs a separate implementation or when the member should be available only through the interface.

Detailed answer

If IAudit.Save() and ICache.Save() mean the same thing, a public Save() can implement both. If they mean different things, implement them explicitly as void IAudit.Save() and void ICache.Save(). Callers must then cast or hold the object as the appropriate interface, which prevents an ambiguous or misleading public API.

Sources