Sobes.tech
Junior — Middle

Can properties or fields be declared in an interface?

sobes.tech AI

Answer from AI

In C# interfaces, you cannot declare fields because an interface only defines a contract, which is a set of methods and properties that must be implemented in classes.

However, properties can be declared in an interface. Properties in an interface are declared without implementation, only with the signature get and/or set:

public interface IExample
{
    int MyProperty { get; set; }
}

A class implementing this interface must provide a concrete implementation of this property.

In summary:

  • Fields cannot be declared in an interface.
  • Properties can be declared in an interface, but without implementation.