Okay, so an int is a value type, and a class is a reference type. How can int be derived from object?
It isn’t, really. When an int is being used as an int, it is a value. However, when it is being used as an object, it is a reference to an integer value (on the managed heap). In other words, when you treat an int as an object, the runtime automatically converts the int value to an object reference. This process is called boxing. The conversion involves copying the int to the heap, and creating an object instance which refers to it. Unboxing is the reverse process – the object is converted back to a value.
Related Questions
- Can I override the equals method or clone method from class Object to take a parameter or return a value of the type that I specify?
- Why is the ListBox displaying the type name of the objects in the list instead of the value of a property in the object?
- Okay, so an int is a value type, and a class is a reference type. How can int be derived from object?