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


Listing 9-25<     > Listing 10-2


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


Example_10_1.cs
// TypeVal is a value type because it's a structure.
struct TypeVal {
   public int m_i;
   public TypeValint i ) { m_i = i; }
}
// TypeRef is a reference type because it's a class.
class TypeRef {
   public int m_i;
   public TypeRefint i ) { m_i = i; }
}
class Program {
   static void Main() {
      TypeVal v1 = new TypeVal(6); // Create an instance of TypeVal.
      TypeVal v2 = v1; // Here, a second instance of 'TypeVal' is 
      // created and its field 'v2.i' is also equal to 6. 
      // However, 'v1' and 'v2' are two different objects. We often 
      // use the term 'variable' to name such instances of value type
      // stored on the stack of its creator thread.
      v2.m_i = 9;
      // Assert that 'v1.i' is equal to 6 and 'v2.i' is equal to 9.
      System.Diagnostics.Debug.Assert( v1.m_i == && v2.m_i == );

      TypeRef r1 = new TypeRef(6); // Create an instance of TypeRef.
      TypeRef r2 = r1; // Here, r2 si a second reference to the object 
      // already referenced by r1. There is only one object and two 
      // references.
      r2.m_i = 9;
      // Assert that both 'r1.i' and 'r2.i' are equal to 9.            
      System.Diagnostics.Debug.Assert( r1.m_i == && r2.m_i == );
   }
}	
Copyright Patrick Smacchia 2006 2007