what's the best method to return a variable length string array
4 Answers
I'd prefer to use a generic collection such as: List<string> (or IList<string>), or IEnumerable<string> depending on how you plan to use it. Generic collections are typically easier to work with than arrays, having a much more robust interface.
7 Comments
user496949
if using generic collection, can I convert it to an array?
Chris Laplante
Yes, by using the
toArray() member method.TeaDrivenDev
Yes, they have a .ToArray() method.
Joel Coehoorn
@user Yes, you can. But don't. In .Net, you should really avoid arrays entirely in favor of collections. Not that you'll never use arrays, but prefer collections as much as possible.
Anero
I usually go with the IEnumerable<string> implementation
|
In the old .NET 1, there was the StringCollection class. it is still supported, and required in a few places, but I would prefer the generic List collection. Or there's the old C-style string[] as per the first answer.
You don't give any specifics, so everything else being equal, I would go for the generic list.