C#
interface implementation
compilation error
public modifier
programming error

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:

csharp
1public interface ILogger
2{
3    void Log(string message);
4}

An explicit implementation looks like this:

csharp
1public class FileLogger : ILogger
2{
3    void ILogger.Log(string message)
4    {
5        Console.WriteLine(message);
6    }
7}

That member is only accessible through an ILogger reference:

csharp
ILogger logger = new FileLogger();
logger.Log("hello");

This will not compile:

csharp
var logger = new FileLogger();
logger.Log("hello");

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:

csharp
1public class FileLogger : ILogger
2{
3    public void ILogger.Log(string message)
4    {
5        Console.WriteLine(message);
6    }
7}

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:

csharp
1void ILogger.Log(string message)
2{
3    Console.WriteLine(message);
4}

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:

csharp
1public interface ITextWriter
2{
3    void Write(string value);
4}
5
6public interface IConsoleWriter
7{
8    void Write(string value);
9}
10
11public class Writer : ITextWriter, IConsoleWriter
12{
13    void ITextWriter.Write(string value) => Console.WriteLine($"Text: {value}");
14    void IConsoleWriter.Write(string value) => Console.WriteLine($"Console: {value}");
15}

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:

csharp
1public class FileLogger : ILogger
2{
3    public void Log(string message)
4    {
5        Console.WriteLine(message);
6    }
7}

Now both of these work:

csharp
1var logger = new FileLogger();
2logger.Log("hello");
3
4ILogger asInterface = logger;
5asInterface.Log("hello");

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 public to 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 public when 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.

Course illustration
Course illustration

All Rights Reserved.