Skip to content

Interface definition

Context: Define an interface using the interface keyword. It can contain methods, properties, events, and indexers.

[access modifier] interface IInterfaceName
{
// method signature
int DoWork(string input);
// property signature
string Name { get; set; }
// event
event EventHandler Updated;
}

Prefix with I (e.g., IComparable, IDisposable).

public interface ILogger
{
void Log(string message);
bool IsEnabled { get; set; }
}

Interfaces can inherit from other interfaces.

public interface IReadable { string Read(); }
public interface IWritable { void Write(string data); }
public interface IReadWrite : IReadable, IWritable { }