This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Download it! (version: 2002.09.16)
This X-Code .NET source template (CustomCollection.cs.xc) takes a single input parameter, a .NET typename T, and emits a strongly-typed collection (in C#) of that type.
This project serves to demonstrate how Generative Programming tools like Gen<X>, TextBox, and X-Code .NET can be used to overcome C#'s lack of generics (or at least its lack of a precompiler) in this one, straightforward, case.
The internal representation of the generated collection class is an array, so the performance characteristics are more akin to a vector than a list, to use STL terminology. The design is optimized for value-types, as it goes to great length to avoid the overhead of boxing and unboxing -- for example, the collection does not simply derive from CollectionBase, which is merely a wrapper around the poorly-typed ArrayList. However, it should work just as well for reference types.
- Minor update to use my new X-Code .NET codegen tool.
- The template now supports a 2nd parameter (name=Access, type=System.Reflection.TypeAttributes) to control the external accessibility of the generated collection class. This parameter, Access, can be either TypeAttributes.Public or TypeAttributes.NotPublic.
- Type-safe implementation of ICloneable interface.
- Major perf. improvement: Leverage Array.Copy for reallocations, insertions, and removals. (Because Array.Copy is implemented in speedy native code.) Credit: Joel Mueller.
- Minor perf. improvement: Removed redundant method implementations, to make less work for the JIT compiler. Credit: Ethan Smith.
- Bug fix: Array now grows properly after removing all items, trimming capacity to zero, then attempting to add new items. Credit: Patrice Lafond.
- Cosmetic fix: Method parameters now have prettier names, for Intellisense purposes. Credit: Joel Mueller.
- An alternate version of the template (CustomCollection.cs.tb) that works with Chris Sells' free TextBox utility (only first few lines differ).
- A single template, for value types and reference types (no more CollectionBase).
- The collection class is now placed in the same namespace as the type it contains (eg: Foo.Bar.Qux will magically become Foo.Bar.QuxCollection).
- Fully tested and benchmarked w/ RTM bits (it screams by ArrayList, for both value types and reference types).
- Exception-throwing characteristics now more closely resemble CollectionBase/ArrayList (think: enumerator versioning).
"So what, exactly, is a strongly-typed .NET collection", you ask? It's an alternative to System.Collections.ArrayList, which deals only in typeless "objects". ArrayList works great, but its typical usage requires quite a bit of casting to appease the compiler. Such wanton downcasting is a very error-prone coding style... the use of a strongly-typed collection class eliminates the need for casting, thus allowing the compiler to catch many common coding errors at compile-time, rather than run-time.
In its defense, the CLR provides an abstract base class (System.Collections.CollectionBase) which takes most of the work out of implementing a type-safe collection. But still, each subclass of CollectionBase requires about 100 lines of repetitive, boilerplate code. Also, CollectionBase is designed with reference-types in mind -- when used with value-types, its implementation will perform lots of needless boxing and unboxing. (The overhead of boxing/unboxing is usually not significant -- but still, there's the principal of the matter.)
Copyright © 2002 by Shawn A. Van Ness. All rights reserved. No warranties extended. Use at your own risk.