Implement Interface Methods the Easy Way





Implement Interface Methods the Easy Way

It can be quick and automatic with Class View.


By Brian Noyes


A common task when creating classes in the .NET Framework is to implement an interface defined already in the Framework, one defined in your own code or in a third-party library. To implement an interface, you must implement all its methods with the proper name, parameters, and return type in your class. Coding this in by hand can be tedious and error-prone. Luckily, a nice little feature in the Class View can automate this process for you.


To see how to do this, start a new C# Class Library project in Visual Studio. The project will start with a class named Class1. Add the interface you want to implement to the class. I implemented the ISerializable interface, defined in the System.Runtime.Serialization namespace.


You class definition should now look like this:


public class Class1 :

  System.Runtime.Serialization.ISerializable {}


Now go to Class View, expand the solution, namespace, and class in the tree. Under the Class1 class in the tree, you'll see a Bases and Interfaces node. If you expand this, you should see the ISerializable interface because the class now derives from it. Under ISerializable, you'll see its single method, GetObjectData (see Figure 1). All you need to do to implement the method is to right-click on it in Class View and select Add > Override from the context menu. The method is added to your class as an empty method with all the right parameters and return type. If the interface had multiple methods, you would simply repeat this process for each to implement each one in turn.


The bad news is that if you are a VB programmer, this feature is not available in Class View. The Class View context menus are different in VB, and unfortunately this item is missing there.


Figure 1. By right-clicking on an interface method for an interface you have derived your class from, you can implement the interface methods quickly by selecting Add > Override from the context menu.


Brian Noyes is an independent software consultant and president of Software Insight ( He's an MCSD with more than 11 years of programming, design and engineering experience. Brian specializes in architecture, design, and coding of cutting-edge software systems, and is a technical editor and frequent contributor to several publications. E-mail him at mailto:[email protected].




Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.