Tuesday, July 26, 2005 9:59 PM dbottjer

Interfaces Defined

It seems possible to write .NET applications and never write interfaces.  It seems less likely to write .NET application and never use an interface.  So what are interfaces and how can they improve your code? 

Essentially, an interfaces is a contract or a template.  Interfaces allow us to define what our objects will do without worrying about how they will do it.  It is important to realize that interfaces are not instantiated like classes, they are just the definition and contain no implementation.  Interfaces are typically defined with a capital "I" followed by a name where the first letter of the name is capitalized.  While classes can only directly inherit from one base class, they can implement a nearly infinate amount of interfaces. 

An interesting use for interfaces is for defining method parameters.  Instead of defining an input parameter as a particular object we can define that parameter as an interface.  This is a powerful concept b/c it allows us to build more flexible methods that will accept any object that implements the same interface as the input parameter.  This same concept can be applied to return types.

Filed under: , ,

Comments

No Comments