Capacity of StringCollection

  • Thread starter Thread starter Dennis Myrén
  • Start date Start date
D

Dennis Myrén

Hi.
I want to know why it is not possible to specify (or determine) the
capacity for a System.Collections.Specialized.StringCollection.

I guess the StringCollection would be the better choice over an
System.Collections.ArrayList
when there is only System.String instances to be stored in it.

The StringCollection provides only one undocumented constructur.

Thank you.
 
Non settable lenght -- it's just the way a collection works.
You can always use an array (string[] stringArr)

Or implement your own collection that inherits from collectionbase and only
allows X elements to be added:

using System.Collections;

public myCollection : CollectionBase
{
private const MAXLENGTH = 50
public Add(srting newItem)
{
if(this.List.Count = MAXLENGTH)
thorw (new Exception("List maxlength encountered")
else
this.List.Add(newItem)
}

}

cheers,
mortb
 
Dennis Myrénwrote:
I guess the StringCollection would be the better choice over an
System.Collections.ArrayList
when there is only System.String instances to be stored in it.

Just be aware that, contrary to what you might expect,
StringCollection is actually slower than ArrayList.
 
Back
Top