O'Reilly logo

A Programmer's Guide to C# 5.0, 4th Edition by Nick Wienholt, Eric Gunnerson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required