9.0 Interfaces

FIle Location

  1. Right click on your solution

  2. Go to Add > New Project and select Console App (.NET Framework)

  3. Name it Interfaces

  4. Right click on the project, go to Add > New Item

  5. Choose Interface

  6. It is customary to name your interface with a capital I, and then a capital for the first letter in the name i.e. ITransactions

Discussion

Interfaces make it easier to maintain a program, especially bigger programs. Interfaces are used to fulfill promises throughout your other classes. Within your interface you will only declare the methods, properties, and events, but you will not do any real implementation until you get to the class. When you get to the class that inherits from the interface, then you will implement exactly what you want your methods, properties, and events to do.

The interface defines the what part of the syntactical contract and the deriving classes define the how part of the syntactical contract.

Within C#, an interface can be defined by using the keyword, interface. For example, the following is an interface for providing transactions:

namespace InterfaceApplication {

   public interface ITransactions {
      // interface members
      void showTransaction();
      double getAmount();
   }

   public class Transaction : ITransactions {
      private string tCode;
      private string date;
      private double amount;

      public Transaction() {
         tCode = " ";
         date = " ";
         amount = 0.0;
      }

      public Transaction(string c, string d, double a) {
         tCode = c;
         date = d;
         amount = a;
      }

      public double getAmount() {
         return amount;
      }

      public void showTransaction() {
         Console.WriteLine("Transaction: {0}", tCode);
         Console.WriteLine("Date: {0}", date);
         Console.WriteLine("Amount: {0}", getAmount());
      }
   }

   class Tester {

      static void Main(string[] args) {
         Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
         Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
         t1.showTransaction();
         t2.showTransaction();
         Console.ReadKey();
      }
   }
}
  1. We defined the interface by typing public interface ITransactions. It is customary to name your interfaces with capitol I to begin the name.

  2. Within our interface we declared any methods, properties, or events we want to promise our future classes to uphold. In this case we want 2 methods, showTransaction() and getAmount().

  3. We made our Transaction class and implemented the interface by typing public class Transaction : ITransaction.

  4. You should then be able press CTRL . to stub out the methods needed to fulfill the promises from your interface.

  5. Lastly, we implemented the showTransaction() and getAmount() methods with what we wanted them to do.

This is smaller example, but once you get into larger scale applications, interfaces can make it much more manageable and faster figuring out what your classes need to get your application to run smoothly and efficiently.

Interfaces can also inherit from other interfaces. Take a look at this example and see how the relationship works:

class Program
    {
        static void Main(string[] args)
        {

            Person person1 = new Person();
            person1.StateName();
        }
    }

    interface IPerson1
    {
        void StateName();
    }

    interface IPerson2 : IPerson1
    {
        void StateLastName();
    }

    interface IPerson3
    {
        void StateAwesome();
    }

    public class Person : IPerson2
    {
        public void StateName()
        {
            Console.WriteLine("My name is Jonas");
        }
        public void StateLastName()
        {
            Console.WriteLine("O'Connor");
        }
    }

    public class Paul : Person, IPerson3
    {
        public void StateAwesome()
        {
            Console.WriteLine("Awesome");
        }
    }
  1. We have three different interfaces: IPerson1, IPerson2, IPerson3

  2. IPerson2 inherits from IPerson1

  3. The class Person inherits from IPerson2 which also inherits from IPerson1. So the promises it has to fulfill are StateLastName() and StateName().

  4. Class Paul inherits from class Person and interface IPerson3. So the promise it has to fulfill is StateAwesome().

  5. Can you see how interfaces can keep your code more organized and readable?

Last updated