#434 – Interfaces
October 17, 2011 3 Comments
An interface is a list of methods, properties, events and indexers that a class may implement. The declaration of an interface looks similar to a class declaration, but doesn’t contain an implementation for any of its members.
public interface ICowHerd { // Properties string HerdName { get; set; } string HerdMotto { get; set; } List<CowInfo> Cows { get; set; } // Methods void DisplayHerdInfo(); void DisplayCowInfo(int cowIndex); // Events event EventHandler<CowAddedEventArgs> CowAdded; }
You can’t do anything with an interface by itself. You can’t instantiate it like a class. Instead, a class can choose to implement an interface, which means that it will define an implementation for every member of the interface. You can think of the interface as a contract that dictates the exact members that a class must implement if it decides to implement the interface.