views:

1984

answers:

5

Hello, 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!
Holger

+23  A: 
  • TStringList is a descendant of TStrings.
  • TStringList knows how to sort itself alphabetically.
  • TStringList has an Objects property.
  • TStringList doesn't make your code incompatible with all previous versions of Delphi.
  • TStringList can be used as a published property. (A bug prevents generic classes from being published, for now.)
Rob Kennedy
+1  A: 
Gerry
+8  A: 

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.

Darian Miller
+1  A: 

I'd probably say if you want backwards compatibility use TStringList, and if you want forward compatibility (perhaps the option to change that list of strings to say list of Int64s in the future) then go for TList.

Steve
A: 

The TStringlist is one very versatile class of Delphi. I used (and abused ;-) ) its Objects property many times. It's very interesting to quickly translate a delimited string to a control like a TMemo and similar ones (TListBox, TComboBox, just to list a few).

I just don't like much TList, as TStringList satisfied my needs without needing of treating pointers (as Tlist is a list of Pointer values).

Fabricio Araujo