Junior — Middle
Can fields be declared inside interfaces?
sobes.tech AI
Answer from AI
In C#, you cannot declare fields (instance variables) inside interfaces. An interface can only contain declarations of methods, properties, events, and indexers without implementation (up to C# 8.0).
This is because an interface defines a contract, not a state.
Starting with C# 8.0, interfaces can contain default method implementations, but fields are still prohibited.
Example of an interface:
public interface IExample
{
int Id { get; set; }
void DoWork();
}
If you need to store state, it is implemented in classes that implement the interface.