Since the early days of templates in the C++ Language, one of the most obvious uses of generic classes has been the definition of generic containers or lists. When you define a list of objects, like Delphi's own TObjectList, in fact, you have a list that can potentially hold objects of any kind. Using either inheritance or composition you can indeed define custom containers for specific a type, but this is a tedious (and potentially error-prone) approach78.
Delphi 2009 defines a small set of generic container classes you can find in the new Generics.Collections unit. The four core container classes are all implemented in an independent way (the is no inheritance among these classes), all implemented in a similar fashion (using a dynamic array), and are all mapped to the corresponding non-generic container class of the Con-tnrs unit: type
78 Here I don't want to cover the difference between the various approaches you could use in Delphi to define specialized container before generics were available, because we do have generics now. You can find material along these lines in my Mastering Delphi 7 book (Sybex) and other editions of that series.
TStack<T> = class TDictionary<TKey,TValue> = class
The logical difference among these classes should be quite obvious considering their names. A good way to test them, is to figure out how many changes you have to perform on existing code that uses a non-generic container class. As an example, I've taken an actual sample program of the Mastering Delphi 2005 book and converted it to use generics79.
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.