The structure in C # in your heart is no more and no less than a bunch of variables glued together with duct tape. If you want each variable of a certain type to represent a bunch of independent but related variables (such as point coordinates) attached with duct tape, it is often better to use an open field structure than a class, regardless of whether a bunch means two or twenty. Note that although the recommendations for a structured Microsoft class are appropriate for data types that encapsulate a single value, it should not be considered applicable to types whose purpose is to encapsulate independent but related values. The greater the degree of independence of the variables, the greater the benefits of using an open field structure.
If you want to use a class to encapsulate a group of independent variables, there are two ways to do this, none of which are terribly convenient. An immutable class can be used, in which case any non-zero storage location of this type of class encapsulates the values stored in the instance identified by it, and one storage location can be copied to another so that a new one encapsulates the same values. Unfortunately, changing one of the values encapsulated by the location of the repository usually requires creating a new instance that is exactly the same as the old one, except that the value has changed. For example, if you have a pt variable of type Immutable3dPoint , and one wanted to increase pt.X by one, you would need to do something like: pt = new Immutable3dPoint(pt.X+1, pt.Y, pt.Z); Perhaps it is permissible if the type only encapsulates three values, but rather annoying if there are a lot of them.
Another class-based approach is to use a mutable class; this usually requires that everyone ensure that each storage location of a class type contains a single reference anywhere in the universe for an instance of that class. When you create a storage location, you need to create a new instance and save the link there. If you want to copy values from storage location P to storage location Q , to another, you need to copy all fields or properties from one instance to another (possibly using a method of type CopyFrom and say Q.CopyFrom(P); Note that if one of these, Q=P; says that may seem to work, but future attempts to change P will also change Q and vice versa. Variable classes can work, and they can be effective from time to time, but it’s very easy to damage things.
Open-field structures combine the convenient semantics of the values of immutable classes with the convenient piecewise modifications allowed by mutable classes. Large structures are copied more slowly than references to immutable objects, but the cost of modifying part of the structure of an open field depends only on the degree of modification, and not on the overall size of the structure. In contrast, the cost of changing one piece of data encapsulated in an immutable class type will be proportional to the total size of the class.
supercat Nov 12 '13 at 9:39
source share