Extension methods on Interfaces
suggest changeAn extension method can give every implementer of an interface the same behavior. Before default interface methods, the interface itself could not supply that body.
public interface IVehicle
{
int MilesDriven { get; set; }
}
public static class Extensions
{
public static int FeetDriven(this IVehicle vehicle)
{
return vehicle.MilesDriven * 5280;
}
}
FeetDriven is available on every IVehicle. One mile is 5280 feet, and that conversion does not have to be copied into each class.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents