Compilation Error The modifier 'public' is not valid for this item while explicitly implementing the interface
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This C# error appears when you combine two incompatible ideas: explicit interface implementation and an access modifier such as public. In C#, an explicitly implemented interface member is not exposed as a normal public member of the class, so the compiler rejects modifiers like public, private, or protected.
What Explicit Interface Implementation Means
Consider this interface:
An explicit implementation looks like this:
That member is only accessible through an ILogger reference:
This will not compile:
because Log is not part of FileLogger's public surface in that form.
Why public Causes the Error
The failing version usually looks like this:
The compiler complains because explicit interface members do not take access modifiers. Their accessibility is defined by the interface contract and by the fact that they are only callable through the interface type.
So the fix is simply to remove public:
When to Use Explicit Implementation
Explicit implementation is useful when:
- two interfaces define members with the same signature
- you want to hide an interface member from the class's public API
- the member is meaningful only when the object is treated as that interface
For example:
Here explicit implementation avoids a naming collision in the public class API.
When You Actually Want a Public Member
If you want callers to invoke the member directly on the class instance, use implicit interface implementation instead:
Now both of these work:
That is the right choice when the method should be part of the class's normal public API.
You Can Combine Both Styles
Sometimes a class exposes a public method and also uses explicit implementation to customize interface behavior. That is legal, but you should do it deliberately because it can be confusing to readers if the two methods behave differently.
In most code, choose one model based on the API you want:
- public member for normal class usage
- explicit member for interface-only access
Common Pitfalls
- Adding
publicto an explicitly implemented interface member. - Choosing explicit implementation when the method really should be callable on the concrete class.
- Forgetting that explicit members require casting to the interface.
- Using both implicit and explicit implementations without a clear reason.
- Treating explicit implementation as an access-control feature rather than an API-shaping feature.
Summary
- Remove
publicwhen implementing an interface member explicitly. - Explicit interface members are accessed through the interface type, not the concrete class.
- Use implicit implementation when you want a normal public class method.
- Use explicit implementation when you need interface-only access or name disambiguation.
- The compiler error is a signal that the chosen implementation style and modifier do not match.

