Boxing Demo

Here are the types to be tested:

    // struct is a value type
    struct PointStruct
    {
      private int x, y;

      public PointStruct(int x, int y)
      {
        this.x = x;
        this.y = y;
      }

      public void Set(int x, int y)
      {
        this.x = x;
        this.y = y;
      }

      public override string ToString()
      {
        return "(" + x.ToString() + ", " + y.ToString() + ")";
      }
    }

    // class is a reference type
    class PointClass
    {
      private int x, y;

      public PointClass(int x, int y)
      {
        this.x = x;
        this.y = y;
      }

      public void Set(int x, int y)
      {
        this.x = x;
        this.y = y;
      }

      public override string ToString()
      {
        return "(" + x.ToString() + ", " + y.ToString() + ")";
      }
    }

Run the tests:

Test PointClass
PointClass pc = new PointClass(1, 2);pc=(1, 2)
Object obj = pc;pc=(1, 2) obj=(1, 2)
((PointClass)obj).Set(3, 4);pc=(3, 4) obj=(3, 4)
pc.Set(5, 6);pc=(5, 6) obj=(5, 6)
pc = obj as PointClass;pc=(5, 6) obj=(5, 6)
pc.Set(7, 8);pc=(7, 8) obj=(7, 8)
((PointClass)obj).Set(9, 0);pc=(9, 0) obj=(9, 0)
Test PointStruct
PointStruct ps = new PointStruct(1, 2);ps=(1, 2)
Object obj = ps;ps=(1, 2) obj=(1, 2)
((PointStruct)obj).Set(3, 4);ps=(1, 2) obj=(1, 2)
ps.Set(5, 6);ps=(5, 6) obj=(1, 2)
ps = (PointStruct)obj;ps=(1, 2) obj=(1, 2)
ps.Set(7, 8);ps=(7, 8) obj=(1, 2)
((PointStruct)obj).Set(9, 0);ps=(7, 8) obj=(1, 2)