How to Resolve Error "CS0686: Accessor cannot implement interface member. Use an explicit interface implementation." in C#
The Compiler Error CS0686 is a naming collision error that occurs when implementing interfaces. The message reads: "Accessor 'method' cannot implement interface member 'member' for type 'interface'. Use an explicit interface implementation."
In C#, properties and events are "syntactic sugar." When you define a property named Value, the compiler generates an underlying method named get_Value (and set_Value).
This error occurs when an interface defines both a property named Name AND a method named get_Name. When you try to implement this class, the compiler cannot generate the property accessor because the name get_Name is already reserved by the method definition.
This guide explains how to resolve this collision using Explicit Interface Implementation.
Understanding Property Accessor Names
When you compile C# code, properties are converted into methods for the Common Language Runtime (CLR).
- Property:
int Data { get; set; } - Compiled Methods:
int get_Data()andvoid set_Data(int value).
If an interface explicitly requests a method named get_Data in addition to the property Data, C# cannot implement the property implicitly because the name get_Data is effectively ambiguous or double-booked.
Scenario: The Name Collision
This situation is rare in pure C# designs but common when importing interfaces from other languages (like C++/CLI or VB.NET) or COM libraries where naming conventions differ.
Example of error: here, the interface demands both a property and a method that compile to the same name.
public interface IExternalData
{
// This expects a method named 'get_Value'
int Value { get; }
// This explicitly defines 'get_Value'
int get_Value();
}
public class DataProvider : IExternalData
{
// ⛔️ Error CS0686: Accessor 'DataProvider.Value.get' cannot implement
// interface member 'IExternalData.Value' for type 'IExternalData'.
// Use an explicit interface implementation.
public int Value
{
get { return 100; }
}
public int get_Value()
{
return 100;
}
}
The compiler tries to generate a get_Value method for the property Value, but it sees you have already defined a method get_Value, causing a conflict in the implementation mapping.
Solution: Explicit Interface Implementation
To resolve this, you must implement the property Explicitly.
Explicit implementation removes the member from the class's public API and hides it behind the interface cast. Internally, the compiler gives the explicit property a distinct name (like IExternalData.get_Value), preventing the collision with the standard method get_Value.
Solution: prefix the property name with the interface name.
public interface IExternalData
{
int Value { get; }
int get_Value();
}
public class DataProvider : IExternalData
{
// ✅ Correct: Implement the PROPERTY explicitly.
// This allows the compiler to handle the naming internaly.
int IExternalData.Value
{
get { return 100; }
}
// ✅ Correct: Implement the METHOD publicly (or explicitly as well).
public int get_Value()
{
return 100;
}
}
Accessing Members:
Because Value is implemented explicitly, you can only access it by casting the object:
((IExternalData)myProvider).Value.
Conclusion
CS0686 is a naming conflict between C# property conventions and explicit method definitions.
- Identify the Conflict: Look at the interface. Does it have
Propandget_Prop? - Use Explicit Implementation: Change
public Type ProptoType InterfaceName.Prop. - Result: The compiler generates unique names for the explicit implementation, avoiding the collision with the standard method.