Sobes.tech
Junior — Middle

Is it possible to declare a variable inside an interface?

sobes.tech AI

Answer from AI

In C#, you cannot declare variables (fields) inside an interface. An interface can only contain declarations of methods, properties, events, and indexers without implementation (up to C# 8.0). Starting from C# 8.0, interfaces can contain methods with default implementations, but fields are still prohibited.

If you need to store state, it should be implemented in the class that implements the interface.

Example of an interface:

public interface ILogger
{
    void Log(string message);
}

Attempting to declare a field inside an interface will result in a compilation error.

Is it possible to declare a variable inside an… - sobes.tech