CHAPTER 9

image

Interfaces

If multiple classes need to share behavior, they can all use the same base class, and that base class may be abstract. But there can be only one base class in C#, and it is often preferable to share behavior without using a base class.

This can be done by defining an interface, which is similar to an abstract class where all methods are abstract.

A Simple Example

The following code defines the interface IScalable and the class TextObject, which implements the interface, meaning that it contains implementations of all the methods defined in the interface.

public class DiagramObject{    public DiagramObject() {}}interface IScalable ...

Get A Programmer's Guide to C# 5.0, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.