It is a reference type
Doing an example with string isn't very illuminating, because string is also a reference type (as is SampleClass
, obviously); your example contains zero "boxing".
if object is reference type then why obj2 value is still "OldString"
Why wouldn't it be? When you create a new string, that doesn't change old references to point at the new string. Consider:
object obj1 = "OldString";
// create a new string; assign obj1 the reference to that new string "OldString"
object obj2 = obj1;
// copy the reference from obj1 and assign into obj2; obj2 now refers to
// the same string instance
obj1 = "NewString";
// create a new string and assign that new reference to obj1; note we haven't
// changed obj2 - that still points to the original string, "OldString"