Alex Rivera | Logout

TStringList vs. TList<string>

Asked 2008-11-10T22:50:25.137
31

what is the difference in using a standard

type 
  sl: TStringList 

compared to using a generic TList

type 
  sl: TList<string>

?

As far as I can see, both behave exactly the same.

Is it just another way of doing the same thing?

Are there situations where one would be better than the other?

Thanks!

Edit
Report

3 Answers

12

TStringList has been around a long time in Delphi before generics were around. Therefore, it has built up a handful of useful features that a generic list of strings would not have.

The generics version is just creating a new type that is identical to TList that works on the type of String. (.Add(), .Insert(), .Remove(), .Clear(), etc.)

TStringList has the basic TList type methods and other methods custom to working with strings, such as .SaveToFile() and .LoadFromFile()

If you want backwards compatibility, then TStringList is definitely the way to go.
If you want enhanced functionality for working with a list of Strings, then TStringList is the way to go. If you have some basic coding fundamentals that you want to work with a list of any type, then perhaps you need to look away from TStringList.

answered 2008-11-11T01:54:10.303
2

From memory point of view TStringList memory usage is increased with the size of TObject pointer added to each item. TList memory usage is increased with the size of pointer added to each item. If is needed just an array of strings without searching, replacing, sorting or associative operations, a dynamic array (array of string) should be just enough. This lacks of a good memory management of TStringList or TList, but in theory should use less memory.

answered 2011-10-17T13:40:54.013
0

TStringList has been used for far too long and has many advantages, all mentioned by Rob Kennedy.

The only real disadvantage of using it as a pair of a string and an object is the necessity of casting object to the actual type expected and stored in this list (when reading) and as far as I know Embarcadero did not provide Delphi 2009 and up VCL libraries with generic version of TStringList.

To overcome this limitation I implemented such list for internal use and for almost 3 years it serves it's purpose so I decided to share it today: https://github.com/t00/deltoo#tgenericstringlist

One important note - it changes the default property from Strings to Objects as in most cases when object is stored in a list it is also the mostly accessed property of it.

answered 2013-07-25T11:00:42.940

Your Answer