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 10-8 extracted from chapter The .NET 2 type system from a C#2 point of view


Listing 10-7<     > Listing 10-9


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


Example_10_8.cs
class Article : System.ICloneable {
   public string Description;
   public int Price;
   public object Clone() {
      return this.MemberwiseClone();
   }
}
class Order {
   public int Quantity;
   public Article Article;
   public override string ToString() {
      return "Order: " + Quantity + " x " + Article.Description + 
             "   Total cost: " + Article.Price * Quantity;
   }
   // Default constructor.
   public Order() { }
   // Copy constructor (parameterized).
   public OrderOrder original , bool bDeepCopy) {
      this.Quantity = original.Quantity;
      if( bDeepCopy )
         this.Article = original.Article.Clone() as Article;
      else
         this.Article = original.Article;
   }
}
class Program {
   static void Main() {
      Order order = new Order();
      order.Quantity = 2;
      order.Article = new Article();
      order.Article.Description = "Shoes";
      order.Article.Price = 80;
      System.Console.WriteLine(order);
      Order orderClone = new Order( order , true );      
      orderClone.Article.Description = "Shirt";
      System.Console.WriteLine(order);
   }
}	
Copyright Patrick Smacchia 2006 2007