2

As the title suggests I would like to know what the difference is between a collection for which I have to set the type of items and all the specialized collections for only one datatype. Does it matter which one I use?

And also is there a difference between List<String> and Collection<String> ?

String is just an example, I'm asking in general.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Andahari
  • 35
  • 1
  • 5

1 Answers1

2

Strongly typed string collection is to prevent upcasting or downcasting overhead and optimized for strings.

The reason for providing StringCollection is to provide better performance. However, I would be surprised if there is a huge performance difference between them.

Consider the following recommendations for using StringCollection: Ref

  • Use StringCollection to store string data that changes frequently and needs to be retrieved in large chunks.
  • Use StringCollection for binding string data to a data grid. This avoids the cost of downcasting it to a string during retrieval.
  • Do not use StringCollection for sorting strings or to store presorted data.
hollystyles
  • 4,979
  • 2
  • 36
  • 38
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • 3
    I just became aware of this answer due to the recent edit. Be aware that the #1 difference is that StringCollection was added before generics, where most code tended to use ArrayList which stored objects, and had to be cast to the right type on use. StringCollection was thus a type-safe collection for strings, and was completely obsoleted when generics was introduced. Most of the answer here seems to relate to the difference between StringCollection and ArrayList or similar, but the difference between StringCollection and the generic list is irrelevant when it comes to performance. – Lasse V. Karlsen Jun 22 '20 at 11:25