#434 – Interfaces

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.

 

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #434 – Interfaces

  1. Pingback: #612 – Members of an Interface Are Implicitly Public « 2,000 Things You Should Know About C#

  2. Pingback: #613 – Interfaces Cannot Contain Static Members « 2,000 Things You Should Know About C#

  3. Pingback: #799 – Interface Members Are Implicitly Public | 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: