Explicit interface implementation
Context: Explicit implementation allows a class to implement interface members without making them public. Useful for resolving naming conflicts or hiding implementation details.
Syntax
Section titled “Syntax”void IInterfaceName.MethodName() { }Example
Section titled “Example”public interface IWriter { void Write(); }public interface IReader { void Write(); } // same name
public class Device : IWriter, IReader{ void IWriter.Write() => Console.WriteLine("Writer write"); void IReader.Write() => Console.WriteLine("Reader write");}Access
Section titled “Access”Explicitly implemented members are only accessible through the interface.
Device d = new Device();// d.Write(); // error((IWriter)d).Write(); // "Writer write"((IReader)d).Write(); // "Reader write"When to Use
Section titled “When to Use”- Resolving name conflicts between interfaces.
- Hiding implementation details (e.g.,
ICollection<T>vsIEnumerable<T>). - Forcing usage through the interface.