How to Resolve Error "CS0754: A partial member may not explicitly implement an interface member" in C#
The Compiler Error CS0754 is a syntax restriction error. The message reads: "A partial member may not explicitly implement an interface member".
In C#, Explicit Interface Implementation allows you to implement a method named in an interface but hide it from the class's public API (accessible only by casting to the interface). Partial Methods allow you to define a method signature in one file and optionally implement it in another.
These two features are mutually exclusive. You cannot combine the partial keyword with the explicit implementation syntax (Return Type InterfaceName.MethodName).
This guide explains how to achieve the goal of partial interface implementation using a "Proxy" or "Hook" pattern.
Understanding the Conflict
- Explicit Implementation:
void IMyInterface.DoWork() { ... } - Partial Method:
partial void DoWork();
The compiler creates special metadata names for explicitly implemented members to hide them. Partial methods rely on strict name matching to pair the definition with the implementation across files. The complexity of resolving names, visibility, and vtables makes combining these two features unsupported.
Scenario: Trying to Split an Interface Implementation
This commonly occurs when working with generated code. You want the generated class to implement an interface, but you want to write the logic for that interface method in a separate, hand-written file.
Example of error
Attempting to declare the explicit implementation as partial.
public interface ICommand
{
void Execute();
}
public partial class Worker : ICommand
{
// ⛔️ Error CS0754: A partial member may not explicitly implement an interface member.
// You cannot prefix a partial method with an interface name ('ICommand.').
partial void ICommand.Execute();
}
Solution: The Partial Hook Pattern
To fix this, you must implement the interface fully in one file (usually the generated one or the main definition), and inside that implementation, call a separate partial method that serves as a "hook."
File 1 (The Interface Implementation):
public partial class Worker : ICommand
{
// 1. Define the partial hook (Definition)
// This allows the logic to be written elsewhere.
partial void OnExecute();
// 2. Implement the interface explicitly
void ICommand.Execute()
{
// 3. Delegate the work to the partial method
OnExecute();
}
}
File 2 (The Logic/Implementation):
public partial class Worker
{
// 4. Implement the partial hook (Implementation)
partial void OnExecute()
{
System.Console.WriteLine("Command Executed!");
}
}
Why this works: The interface requirements are satisfied by void ICommand.Execute(), which is a valid, non-partial method. The internal logic is split using the standard partial void OnExecute() mechanism, which is valid because it is a private member of the class, unrelated to the interface directly.
Conclusion
CS0754 prevents invalid syntax combinations.
- Don't Mix: Never write
partial InterfaceName.MethodName. - Use a Proxy: Implement the interface explicitly (
void Interface.Method()) and have it call a distinctpartial void MethodHook(). This achieves the same goal of separating the definition from the logic.