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


Listing 10-36<     > Listing 10-38


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


Example_10_37.cs
using System;
public class Article {
   public int m_Price = 0;
   public Articleint price ) { m_Price = price; }
   public int IncPrice( int i ) {
      m_Price += i;
      return m_Price;
   }
}
public class Program {
   public delegate int Deleg(int i);
   public static void Main() {
      Article a = new Article100 );
      Article b = new Article103 );
      Article c = new Article107 );

      // 'deleg' references ( a.IncPrice , b.IncPrice , c.IncPrice ).
      Deleg deleg = a.IncPrice;
      Deleg deleg1 = b.IncPrice;
      deleg1 += c.IncPrice;
      deleg += deleg1;
      deleg( 10 );
      Console.WriteLine( "a:{0} b:{1} c:{2}", 
                         a.m_Price , b.m_Price , c.m_Price );

      // Try to supress the sub list ( a.IncPrice , c.IncPrice )
      // that cannot be found in the delegate object 'deleg'.
      Deleg deleg2 = a.IncPrice;
      deleg2 += c.IncPrice;
      deleg -= deleg2;
      deleg(10);
      Console.WriteLine( "a:{0} b:{1} c:{2}",
                         a.m_Price , b.m_Price , c.m_Price );

      // Try to supress the sub list ( a.IncPrice , b.IncPrice )
      // that can be found in the delegate object 'deleg'.
      Deleg deleg3 = a.IncPrice;
      deleg3 += b.IncPrice;
      deleg -= deleg3;
      deleg(10);
      Console.WriteLine( "a:{0} b:{1} c:{2}", 
                         a.m_Price , b.m_Price , c.m_Price);
   }
}	
Copyright Patrick Smacchia 2006 2007