"Sample Code"
1 usingSystem;2 //Point is a Value Type3 Internal structPoint {4 PrivateInt32 m_x, m_y;5 6 PublicPoint (Int32 X,int32 y) {7m_x =x;8M_y =y;9 }Ten One Public voidChange (Int32 x, Int32 y) { Am_x =x; -M_y =y; - } the - Public OverrideString ToString () { - returnString.Format ("({0}, {1})", m_x, m_y); - } + - Public Sealed classProgram { + Public Static voidMain () { APoint P =NewPoint (1,1); at - Console.WriteLine (P); //1,1 with boxed Operation Console.WriteLine (Object) - -P.change (2,2); - Console.WriteLine (P); //2,2 with boxed Operation Console.WriteLine (Object) - inObject o =p; //With boxing operation - Console.WriteLine (o); //2,2 No boxing to +(point) O). Change (3,3); //unpacking and copying the point object to a temporary variable on the stack and executing the change method - Console.WriteLine (o); //2,2 the } * } $}
Boxing, unpacking exercises