Until now, all the class types I've used in this series have been ref classes, which means that instances of them-including those declared on the stack-are managed by the garbage collector. This month, I look at what is often referred to as a "lightweight" class mechanism; namely, the value class, instances of which are not managed by the garbage collector. Value class types are particularly useful for reasonably small data structures that have value semantics. Examples include points in a coordinate system and complex numbers. Typically, a good candidate for implementation as a value class will have only a few data members, will not require inheritance, and will not be expensive in terms of passing and returning by value or copying during assignment.
展开▼