Saturday, August 19, 2006
Reference to the wise
Hardly a better example can be found than documentation of the ASP.NET 2.0 collection classes. Many such classes, including all the hobbled C++ imitators that Microsoft calls "generic collections" in C#, accept objects -- or rather, references to objects. What they collect is merely the references. This probably seemed so obvious to Microsoft writers that they did not think to mention it, but neither did the redoubtable Herb Schildt, in his book on C# 2.0 laughingly labeled "The Complete Reference."
Although it is possible to write copy-constructors in C# just as one usually does in C++, Microsoft does not provide default copy-constructors or use copy-constructors in its ASP.NET interfaces. Omission of copy-constructors as a routine practice makes it tricky to produce collections like those of C++ providing copy-semantics. Microsoft does not seem to have tried. Its practices extend to the collections implemented for visual controls such as TemplateGroupCollection, yielding interesting consequences.
Experiments suggest that all ASP.NET collections accepting reference types will accept duplicate references without an exception and are unprotected against changes to the referenced objects after they have been added. In some situations, such as user interfaces, effects of adding duplicate references or changing an object after a reference to it has been added can depend on timing and may be unpredictable.
In all but special circumstances, therefore, rules of thumb for peaceful coexistence with ASP.NET 2.0 collections have to be: add only unique objects, and don't change objects after adding them to these collections.
Subscribe to Posts [Atom]