TStringList vs. TList<string>
Asked Answered
D

8

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!

Daile answered 10/11, 2008 at 22:50 Comment(1)
Try to add 'Hello' + sLineBreak + 'World' to both of them. The behavior is not the sameNanine
E
42
  • 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.)
Emileeemili answered 10/11, 2008 at 22:58 Comment(3)
The fact that TStringList is a descendant of TStrings is a major feature: it can be used in the VCL where a TStrings is used. So it is very handy to use TStrings in your code.Outgo
You're responding to an answer that's five years old, @Bernd. If you want to know whether published properties can have types that are generic classes, I invite you first to try it out, and second to post a real question asking about it. When you post your question, you'll be expected to have tried it first, so make sure to report your experience, and mention which version you're using. In 2008, I obviously wasn't talking about any version newer than Delphi 2009.Emileeemili
That's a good argument! I didn't enable my brain. Of curse there can't be an other version than 2009, because with that version where the generics introduced and no other version of Delphi was published to the marked at that time. But now we have it documented ;-)Harryharsh
S
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.

Sackcloth answered 11/11, 2008 at 1:54 Comment(0)
L
2
  • As TStringList is a descendant of TStrings it is compatible with the Lines property of TMemo, Items of TListbox and TComboBox and other VCL components. So can use cbList.Items := StringList; // internally calls TStrings.Assign
Latinalatinate answered 11/11, 2008 at 1:11 Comment(1)
but you can do cbList.Items.AddStrings(ListOfString.toArray) or somethign like that. PS. The real question to bring in what is different between TList<string> and TArray<string> :-DDye
B
2

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.

Bancroft answered 11/11, 2008 at 8:34 Comment(0)
C
2

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

EDIT: I confused the TList(list of pointers) with TList (generic list of strings). Sorry for that. My point stands: TStringList is just a lot more than a simple list of strings.

Caesaria answered 12/11, 2008 at 17:15 Comment(0)
B
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.

Barren answered 17/10, 2011 at 13:40 Comment(0)
A
1

For most purposes that TStringList has been abused in the past, TObjectDictionary is better - it's faster and doesn't need sorting.

If you need a TStrings object (generally for UI stuff, since the VCL doesn't use generics much even for XE5) use TStringList - the required casting from TObject is annoying but not a showstopper.

Anthroposophy answered 27/9, 2013 at 11:16 Comment(0)
M
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.

Mittiemittimus answered 25/7, 2013 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.