C Sharp

Interface Use

To understand where interfaces are useful, let's first look at a traditional programming problem in Microsoft Windows development that doesn't use an interface but in which two pieces of disparate code need to communicate in a generic fashion. Imagine that you work for Microsoft and you are the lead programmer for the Control Panel team. You need to provide a generic means for any client applet to bolt into Control Panel so that its icon appears in Control Panel and the end user can execute the program. Keeping in mind that this functionality was designed before COM was introduced, how would you provide a means for any future application to be integrated into Control Panel? The solution that was conceived has been a standard part of Windows development for years.

You, as lead programmer for the Control Panel team, design and document the function (or functions) that you need the client application to implement, along with a few rules. In the case of Control Panel applets, Microsoft determined that to write a Control Panel applet you need to create a dynamic-link library (DLL) that implements and exports a function named CPIApplet. You also need to append this DLL's name with an extension of .cpl and place the DLL in the Windows System32 folder. (On Windows ME or Windows 98, this will be Windows\System32, and on Windows 2000, this will be WINNT\System32.) Once Control Panel loads, it loads all DLLs in the System32 folder with the .cpl extension (by using the LoadLibrary function) and then uses the GetProcAddress function to load the CPIApplet function, thereby verifying that you've followed the rules and can properly communicate with Control Panel.

As I mentioned, this is a standard programming model in Windows for handling situations in which you have a piece of code that you want future unknown code to communicate with in a generic manner. However, this isn't the most elegant solution in the world, and it definitely has its problems. The biggest disadvantage to this technique is that it forces the client-the Control Panel code, in this case-to include a lot of validation code. For example, Control Panel couldn't just assume that any .cpl file in the folder is a Windows DLL. Also, Control Panel needs to verify that the correction functions are in that DLL and that those functions do what the documentation specifies. This is where interfaces come in. Interfaces let you create the same contractual arrangement between disparate pieces of code but in a more object-oriented and flexible manner. In addition, because interfaces are a part of the C# language, the compiler ensures that when a class is defined as implementing a given interface, the class does what it says it does.

In C#, an interface is a first-class concept that declares a reference type that includes method declarations only. But what do I mean when I say "first-class concept"? The term means that the feature in question is a built-in, integral part of the language. In other words, it's not something that was bolted on after the language was designed. Now let's delve deeper into the details of what interfaces are and how to declare them.

NOTE
To the C++ developers in attendance, an interface is basically an abstract class with only pure virtual methods declared in addition to other C# class member types, such as properties, events, and indexers.
by BrainBellupdated
Advertisement: