Interfaces:Working with Interfaces : Interface « Class Interface « C# / C Sharp

Interfaces:Working with Interfaces

Interfaces:Working with Interfaces

A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3

// 10 - Interfaces\Working with Interfaces
// copyright 2000 Eric Gunnerson
using System;

public class WorkingwithInterfaces
    public static void Main()
        DiagramObject[] dArray = new DiagramObject[100];
        dArray[0] = new DiagramObject();
        dArray[1] = new TextObject("Text Dude");
        dArray[2] = new TextObject("Text Backup");
        // array gets initialized here, with classes that
        // derive from DiagramObject. Some of them implement
        // IScalable.
        foreach (DiagramObject d in dArray)
            if (d is IScalable)
                IScalable scalable = (IScalable) d;

interface IScalable
    void ScaleX(float factor);
    void ScaleY(float factor);
public class DiagramObject
    public DiagramObject() {}
public class TextObject: DiagramObject, IScalable
    public TextObject(string text)
        this.text = text;
    // implementing IScalable.ScaleX()
    public void ScaleX(float factor)
        Console.WriteLine("ScaleX: {0} {1}", text, factor);
        // scale the object here.
    // implementing IScalable.ScaleY()
    public void ScaleY(float factor)
        Console.WriteLine("ScaleY: {0} {1}", text, factor);
        // scale the object here.
    private string text;


Related examples in the same category

1.Interface with method name conflicts
2.Demonstrate the ByTwos interfaceDemonstrate the ByTwos interface
3.Demonstrate the ByTwos interface 2Demonstrate the ByTwos interface 2
4.Use a property in an interfaceUse a property in an interface
5.Add an indexer in an interfaceAdd an indexer in an interface
6.One interface can inherit anotherOne interface can inherit another
7.Explicitly implement an interface memberExplicitly implement an interface member
8.Use explicit implementation to remove ambiguityUse explicit implementation to remove ambiguity
9.Two class inherit one interfaceTwo class inherit one interface
10.Using interface 3Using interface 3
11.illustrates interfacesillustrates interfaces
12.illustrates implementing multiple interfacesillustrates implementing multiple interfaces
13.illustrates inheriting from a class and implementing multiple interfacesillustrates inheriting from a class and implementing multiple interfaces
14.illustrates casting an object to an interfaceillustrates casting an object to an interface
15.illustrates deriving an interface from one interfaceillustrates deriving an interface from one interface
16.illustrates deriving an interface from multiple interfacesillustrates deriving an interface from multiple interfaces
17.illustrates an explicit interface member implementationillustrates an explicit interface member implementation
18.illustrates interface member hidingillustrates interface member hiding
19.Demonstrates the use of a simple interfaceDemonstrates the use of a simple interface
20.Interface demoInterface demo
21.Interface demo: implements two interfacesInterface demo: implements two interfaces
22.Interface castingInterface casting
23.Overriding InterfacesOverriding Interfaces
24.Overriding Interfaces: Tester Overriding InterfacesAsOverriding Interfaces: Tester Overriding InterfacesAs
25.A safe method of determining whether a class implements a particular interfaceA safe method of determining whether a class implements a particular interface
26.Reimplementation of Interfaces
27.Interface with event