Interface casting : Interface « Class Interface « C# / C Sharp






Interface casting

Interface casting
/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace InterfaceDemo
 {
     interface IStorable
     {
         void Read();
         void Write(object obj);
         int Status { get; set; }

     }

     // here's the new interface
     interface ICompressible
     {
         void Compress();
         void Decompress();
     }


     // Document implements both interfaces
     class Document : IStorable
     {
         // the document constructor
         public Document(string s)
         {
             Console.WriteLine("Creating document with: {0}", s);

         }

         // implement IStorable
         public void Read()
         {
             Console.WriteLine(
                 "Implementing the Read Method for IStorable");
         }

         public void Write(object o)
         {
             Console.WriteLine(
                 "Implementing the Write Method for IStorable");
         }

         public int Status
         {
             get { return status; }
             set { status = value; }
         }

         // hold the data for IStorable's Status property
         private int status = 0;
     }


    public class TesterInterfaceDemoCasting
    {

       [STAThread]
       static void Main()
       {
           Document doc = new Document("Test Document");

           // only cast if it is safe
           if (doc is IStorable)
           {
               IStorable isDoc = (IStorable) doc;
               isDoc.Read();
           }
           else
           {
               Console.WriteLine("Could not cast to IStorable");
           }

           // this test will fail
           if (doc is ICompressible)
           {
               ICompressible icDoc = (ICompressible) doc;
               icDoc.Compress();
           }
           else
           {
               Console.WriteLine("Could not cast to ICompressible");
           }       }
    }
 }

           
       








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.Overriding InterfacesOverriding Interfaces
23.Overriding Interfaces: Tester Overriding InterfacesAsOverriding Interfaces: Tester Overriding InterfacesAs
24.A safe method of determining whether a class implements a particular interfaceA safe method of determining whether a class implements a particular interface
25.Interfaces:Working with InterfacesInterfaces:Working with Interfaces
26.Reimplementation of Interfaces
27.Interface with event