Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TStringList vs. TList<string>

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!

like image 261
Holgerwa Avatar asked Nov 10 '08 22:11

Holgerwa


People also ask

How many strings can a TStringList hold?

TStringList can hold up to 134,217,728 strings (MaxListSize+1).

What is TStringList in Delphi?

TStringList maintains a list of strings. Use a string list object to store and manipulate a list of strings. TStringList implements the abstract properties and methods introduced by TStrings, and introduces new properties, events, and methods to: Sort the strings in the list. Prohibit duplicate strings in sorted lists.

How many strings can a TStringList hold in C++ Builder?

TStringList can contain up to 2,147,483,647 strings. This is because the list is indexed using an integer. See String Types (Delphi) for information about the size limit of each string.


2 Answers

  • 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.)
like image 198
Rob Kennedy Avatar answered Sep 18 '22 12:09

Rob Kennedy


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.

like image 29
Darian Miller Avatar answered Sep 18 '22 12:09

Darian Miller