Strongly-typed Collection Template for .NET

codegen technology to make up for C#'s lack of templates

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.

Mad props to Nick Wienholt, Kit George, Joel Mueller, Ethan Smith, Chris Sells, and Patrice Lafond for assisting me, at various times and in various ways.

What's New:






"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.)

Known Limitations and Bugs:


Copyright © 2002 by Shawn A. Van Ness.  All rights reserved.  No warranties extended.  Use at your own risk.