Developer

Create custom class interfaces in .NET

Harold Davis returns to the topic of .NET class interfaces with a look at creating your own interfaces in C#. You'll learn how to specify an interface--and you'll have a nice chunk of code to download and play with.


Implementing the interfaces that come with .NET allows you to deploy your classes with standardized functionality that other developers expect. In my previous article, I introduced class interfaces and compared them to abstract base classes. I also described the most important .NET interfaces and created a class that implements the IComparable interface.

Now it's time to take things a step further and create a custom class. Creating your own classes will enable you to enforce best practices and standardize your development efforts. Let's review the class I created last time. Then, we'll see what's involved in creating a custom class. You can download the complete code for this article here.

A quick recap
The Dinosaur class implements the IComparable interface (as you can see in the class declaration):
public class Dinosaur : IComparable

The IComparable interface requires that implementing classes include a CompareTo method. I did this rather trivially, simply passing on to the class a String comparison based on the name of the class instance:
 
public int CompareTo (object obj) {
   Dinosaur dino = (Dinosaur) obj;
   return this.Name.CompareTo (dino.Name);
}

 

Listing A shows the full Dinosaur class as it currently stands.

In addition, a bunch of derived Dinosaur classes are running around, as shown in Listing B.

Since the derived classes each implement IComparable—via the inherited Dinosaur class—they can be sorted in an array (Listing C) and displayed in a ListBox in order, as shown in Figure A.

Figure A


Specifying an interface
Let’s move on from here with a custom interface, ICarnivore, instead of using an interface that comes out of the .NET Framework box. To begin with, an interface is specified using the interface keyword and contains the signatures of the interface members. For example, if the ICarnivore interface contained one method, CanIEatU, the interface specification might look like this:
interface ICarnivore {
   bool CanIEatU (object obj);
}


This interface specification says that there is one method, of type bool, that takes a parameter of type object. It’s worth emphasizing that the interface specification contains not a jot or iota of implementation code. It simply says: You have committed to implement me and therefore you must have a method named CanIEatU that takes an object argument and returns a bool.

Adding the interface to the class
Once you specify the interface, you can instruct a class to implement it in its declaration in the inheritance clause. Since classes can implement multiple interfaces—as opposed to being able to inherit from only one class—the interface implementation is specified in a comma-delimited list. For example:
public class Dinosaur : IComparable, ICarnivore
{
   ...
}


As with the IComparable interface, if you run this code without placing the member required by ICarnivore in the class, you’ll get a syntax error, as shown in Figure B.

Figure B


Implementing the ICarnivore interface
Implementing ICarnivore requires adding an appropriate CanIEatU method to the Dinosaur class (from which all the other classes inherit). To go with something pretty simple, I’ve used the Dinosaur class Length field: If I’m longer than you, I can eat you, as shown in Listing D.

Listing E, the complete Dinosaur class, implements both IComparable and ICarnivore.

It’s sometimes helpful to use the Object Browser to display the interfaces implemented by a class—which includes custom interfaces such as ICarnivore (Figure C).

Figure C


Once you implement an interface, it’s easy to use its functionality, because you know that the functionality is going to be there. The code in Listing F creates two dinosaurs, determines who is going to eat whom for dinner using a class instance’s CanIEatU method, and displays the results, as shown in Figure D.

Figure D


Standards and practices
As you can see from this article and the previous one, implementing existing .NET interfaces is a good way to make sure that your classes are deployed with standardized functionality that other developers expect. But you're not limited to using only those interfaces that .NET includes. Implementing your own custom interfaces provides an excellent way to enforce best practices and standardized development across a team—likely with more functionality than the Dinosaur classes shown in this tutorial.

Harold Davis is the author of many books about programming, including  Visual Basic .NET Programming and Visual C# .NET Programming, both published by Sybex.

Editor's Picks