How to Resolve Error "CS0524: 'type' : interfaces cannot declare types" in C#
The Compiler Error CS0524 is a structural restriction error. The message reads: " 'InterfaceName' : interfaces cannot declare types".
In C#, an interface is a contract that defines a set of behaviors (methods, properties, events, indexers) that implementing classes must provide. While classes and structs can contain nested types (e.g., a class inside a class), interfaces are strictly forbidden from containing nested type definitions. You cannot define a class, struct, enum, or delegate inside an interface block.
This guide explains where to place these types to resolve the error.
Understanding Interface Content
Interfaces are designed to be lightweight definitions of a surface area.
- Allowed: Methods, Properties, Events, Indexers. (And since C# 8.0, static fields/methods and default implementations).
- Not Allowed: Nested Types. This includes
class,struct,enum,interface, anddelegate.
If you try to nest a type definition inside the curly braces of an interface, the compiler raises CS0524.
Scenario 1: Defining Enums Inside Interfaces
This is the most common cause. A developer creates an interface and wants to define an enum that is used as a return value for one of the interface's methods. Logically, it feels like the enum belongs to the interface, but syntactically, it must be outside.
Example of error
public interface IMachine
{
// ⛔️ Error CS0524: interfaces cannot declare types.
// You cannot define 'State' inside 'IMachine'.
public enum State
{
On,
Off,
Error
}
State GetCurrentState();
}
Solution: Move to Namespace Level
Move the enum outside of the interface, directly into the namespace. This makes it accessible to both the interface and the classes implementing it.
namespace MyApp.Hardware
{
// ✅ Correct: The Enum lives in the namespace
public enum MachineState
{
On,
Off,
Error
}
public interface IMachine
{
// ✅ Correct: The interface references the external type
MachineState GetCurrentState();
}
}
Scenario 2: Defining Helper Classes/Structs
Sometimes you might try to define a small data structure (like a parameter object or a result struct) inside the interface to keep things "organized."
Example of error
public interface IDataRepository
{
// ⛔️ Error CS0524: interfaces cannot declare types.
public struct SaveResult
{
public bool Success;
public int Id;
}
SaveResult Save(string data);
}
Solution: Move Out or Use a Wrapper Class
Move the struct outside. If you really want to scope the struct to avoid namespace pollution, you can nest it inside a concrete Class, but never inside an interface.
Option A: Namespace Scope (Standard)
// ✅ Correct
public struct SaveResult
{
public bool Success;
public int Id;
}
public interface IDataRepository
{
SaveResult Save(string data);
}
Option B: Class Nesting (If scoping is strict)
If the type implies implementation details, maybe it belongs in a class, and the interface should use a more generic type or a separate DTO.
C# 8.0 Default Interface Members: Even though C# 8.0 introduced the ability to write code (implementation logic) inside interfaces, it did not lift the restriction on declaring nested types. You still cannot define a class inside an interface.
Conclusion
CS0524 enforces the role of interfaces as contracts, not containers.
- Identify the Type: Are you defining an
enum,struct, orclass? - Check the Scope: Is it inside
interface MyInterface { ... }? - The Fix: Cut the type definition and Paste it outside the interface (usually immediately before or after it in the same file).