Interfaces
Prerequisites: Abstract classes
An interface defines a contract with no fields of its own — a
class can implement any number of them, exactly like Java.
interface IPayable {
double CalculatePay();
}
interface INamed {
string GetName();
}
class Employee : IPayable, INamed {
private string _name;
private double _hourlyRate;
private int _hoursWorked;
public Employee(string name, double hourlyRate, int hoursWorked) {
_name = name;
_hourlyRate = hourlyRate;
_hoursWorked = hoursWorked;
}
public double CalculatePay() => _hourlyRate * _hoursWorked;
public string GetName() => _name;
}
C# convention prefixes interface names with I (IPayable,
IDisposable) — a naming convention, not a language requirement,
but followed almost universally in real C# code.