Skip to content

Interfaces

Interfaces define additional sets of functionality that classes can implement; however, interfaces contain no implementation except for static methods and static fields. An interface specifies a contract that a class implementing the interface must follow. Interfaces can contain shared (known as "static" in many compiled languages) or instance methods, shared fields, properties, and events. All interface members must be public. Interfaces cannot define constructors. The .NET runtime allows an interface to require that any class that implements it must also implement one or more other interfaces.

When you define a class, you list the interfaces which it supports following a colon after the class name. The value of ⎕USING (possibly set by :Using) is used to locate interface names.

If you specify that your class implements a certain interface, you must provide all of the members (methods, properties, and so on) defined for that interface. However, some interfaces are only marker interfaces and do not specify any members.

Example

The TemperatureConverterCtl2 custom control derives from System.Web.UI.Control. The first line of this class definition reads:

:Class TemperatureConverterCtl2: System.Web.UI.Control, System.Web.UI.IPostBackDataHandler, System.Web.UI.IPostBackEventHandler

Following the colon, the first name is the base class. After the (optional) base class name is the list of interfaces that are implemented. The TemperatureControlCtl2 custom control implements two interfaces called IPostBackDataHandler and IPostBackEventHandler. These interfaces are required for a custom control that intends to render the HTML for its own form elements in a web page. They define certain methods that get called at the appropriate time by the page framework when a web page is constructed for the browser. It is, therefore, essential that the class implements all the methods specified by the interface, even if they do nothing.

The base class, System.Web.UI.Control, defines an optional interface called INamingContainer. A class based on Control that implements INamingContainer specifies that its child controls are to be assigned unique ID attributes within an entire application. This is a marker interface with no methods or properties defined for it.