How to Resolve Error "CS0540: Class.Member containing type does not implement interface member" in C#
The Compiler Error CS0540 is a logical consistency error regarding Explicit Interface Implementation. The message reads: "'Class.Member' : containing type does not implement interface 'Interface'".
In C#, you can implement an interface member explicitly by prefixing the name (e.g., void ILogger.Log()). However, for this syntax to be valid, the class itself must explicitly declare that it implements that specific interface in its class definition line (e.g., class MyClass : ILogger).
The compiler is essentially saying: "You are writing code to implement ILogger's method, but you never claimed that this class implements ILogger."
This guide explains how to link the class declaration with the member implementation.
Understanding the Requirement
To use Explicit Interface Implementation syntax:
ReturnType InterfaceName.MethodName(Params)
The class definition line must look like this:
class ClassName : InterfaceName
If InterfaceName is missing from the list after the colon :, the explicit implementation is considered an orphan code block targeting a contract that doesn't exist for this class.
Scenario 1: Missing Interface in Class Declaration
This is the most direct cause. You copied a method implementation into your class but forgot to update the class signature to include the interface.
Example of error
public interface IPrinter
{
void Print();
}
// ⛔️ Error CS0540: 'MyPrinter.IPrinter.Print()': containing type does not implement interface 'IPrinter'.
// The class declaration is missing ': IPrinter'.
public class MyPrinter
{
void IPrinter.Print()
{
Console.WriteLine("Printing...");
}
}
Solution: Add the Interface
Add the interface name to the class declaration.
// ✅ Correct: The class claims to implement IPrinter
public class MyPrinter : IPrinter
{
void IPrinter.Print()
{
Console.WriteLine("Printing...");
}
}
Scenario 2: Re-implementing Interfaces in Derived Classes
This is a subtle and common scenario involving inheritance.
If BaseClass implements IInterface, DerivedClass inherits that implementation. However, if DerivedClass wants to provide a new Explicit Implementation for that interface, it must explicitly list the interface again in its own declaration. Just inheriting it from the base is not enough to allow the explicit syntax.
Example of error
public interface IWorker { void Work(); }
public class Employee : IWorker
{
public void Work() { }
}
// Manager inherits IWorker via Employee
public class Manager : Employee
{
// ⛔️ Error CS0540: 'Manager' inherits IWorker, but to use explicit syntax
// 'IWorker.Work', it must list IWorker directly in the class line.
void IWorker.Work()
{
Console.WriteLine("Managing...");
}
}
Solution: Re-list the Interface
Add the interface to the derived class to tell the compiler you are re-implementing it.
// ✅ Correct: Explicitly listing 'IWorker' again allows explicit implementation.
public class Manager : Employee, IWorker
{
void IWorker.Work()
{
Console.WriteLine("Managing...");
}
}
Scenario 3: Generic Type Mismatch
If you are implementing a generic interface, the type arguments in the class declaration and the method implementation must match exactly.
Example of error
The class claims to implement List<int>, but the method tries to implement List<string>.
using System.Collections.Generic;
// Class implements IEnumerable of INT
public class NumberList : IEnumerable<int>
{
// ... other methods ...
// ⛔️ Error CS0540: The class implements IEnumerable<int>,
// but this method tries to implement IEnumerable<string>.
IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
return null;
}
}
Solution: Match Generics
Ensure the types align.
public class NumberList : IEnumerable<int>
{
// ...
// ✅ Correct: Matches the class declaration
IEnumerator<int> IEnumerable<int>.GetEnumerator()
{
return null; // Implementation...
}
}
Conclusion
CS0540 ensures that your code structure matches your class definition.
- Check Declaration: Look at
class MyClass : .... Is the interface listed there? - Check Inheritance: If you are in a subclass, did you re-list the interface to allow explicit overriding?
- Check Generics: Do the
<T>parameters in the method match the ones in the class header?