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


Listing 10-4<     > Listing 10-6


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


Example_10_5.cs
class Article {
   public string Description;
   public int Price;
}
class Order : System.ICloneable {
   public int Quantity;
   public Article Article;
   public override string ToString() {
      return "Order: " + Quantity + " x " + Article.Description + 
             "   Total cost: " + Article.Price * Quantity;
   }
   public object Clone() {
      Order clone = new Order();
      // Shallow copy
      clone.Quantity = this.Quantity;
      clone.Article = this.Article;
      return clone;
   }
}
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 = order.Clone() as Order;
      orderClone.Article.Description = "Shirt";
      System.Console.WriteLine( order );
   }
}	
Copyright Patrick Smacchia 2006 2007