Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 12-13 extracted from chapter Inheritance, polymorphism and abstraction


Listing 12-12<     > Listing 12-14


This listing can be compiled with the command line:
csc.exe /target:exe Example_12_13.cs
Errors: 0 Warnings: 0


Example_12_13.cs
interface I {
   void SetState(int i);
   int GetState();
}
struct Struct : I {
   private int i;
   public void SetState(int i) { this.i = i; }
   public int GetState() { return i; }
}
class Program {
   static void Main() {
      Struct s = new Struct();
      // Here, there is an implicit boxing of the instance 's'.
      I i = (I) s;
      s.SetState(10);
      i.SetState(20);
      System.Console.WriteLine( "s.GetState() returned:" + s.GetState() );
      System.Console.WriteLine( "i.GetState() returned:" + i.GetState() );
   }
}	
Copyright Patrick Smacchia 2006 2007