crunchy
crunchy

Reputation: 703

best practices for handling list of strings

I have to rewrite some old code and I'm wondering the best way to handle a long list of strings. The list is comprised of around 100 items, and each item is used to match a folder which may contain several files that will be added to a zip archive which will be named "[parentfolder].zip"

The current code looks something like this:

    string[5] list = {"a", "b", "c", "d", "e"};

    for (int i = 0; i < 5; i++){
        // open folder for list[i]
        if (Directory.Exists(string.Format("c:\\{0}", i))){
            // get files and add them to list[i].zip

            // do some work with archive
        }
    }

The list of folder names is not likely to change, but there is a small possibility that it will.

My question is what would be the best way to handle the list of folders? An array of strings is obviously not optimal. I was thinking of using an Enum, but would welcome any other suggestions as this 'list' seems like it could be easy to make errors in

(the item names are usually only three letters btw)

Sorry if this seems like a dumb question ;)

EDIT: orginally stated that the list of names is not likely to change, this was very badly stated as there is a small chance that the list will be added to.

Upvotes: 0

Views: 1163

Answers (5)

Guffa
Guffa

Reputation: 700322

Why do you think that an array of strings is not optimal?

The array is the simplest form of collection, and is used as internal storage for most collections, like List<T> and Dictionary<T>. As an array covers your needs, that should be the obvious choise.

You can use an enumerator to loop the array, which makes the code a bit cleaner:

string[] list = {"a", "b", "c", "d", "e"};

foreach (string name in list) {
    // open folder for list[i]
    if (Directory.Exists(string.Format("c:\\{0}", name))){
        // get files and add them to list[i].zip

        // do some work with archive
    }
}

Note: In your original code you were using i instead of list[i] to get the name. Also, you should use list.Length instead of 5 to determine the length of the loop.

Upvotes: 2

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112342

An array is kind of semi-dynamic. You don't have to know its size at design time, however you can't change its size after you have created it. A List however can grow dynamically and is widely used in the .NET Framework. I don't see any problems in using lists. Enums are something completely different. They represent a set of named constants.

List<string> directories = new List<string>();
int i = 0;
while (true) {
    string dir = string.Format("c:\\{0}", i++);
    if (Directory.Exists(dir)) {
        directories.Add(dir);
    } else {
        break;
    }
}

EDIT: Note I am not sure, if your code example is accurate. If list is supposed to contain the directory names to test for, then you would have to build the name of the directory with
string.Format("c:\\{0}", list[i]):

Upvotes: 0

Matthew Cox
Matthew Cox

Reputation: 13672

When I encounter scenarios like this I tend to prefer writing them to an file (xml or text). This also adds the advantage of not having to update code if folder names change or if new ones need to be introduced. You mentioned that neither of those two cases are likely to occur so the benefit is somewhat lost. This option is slower because of the IO operations involved in reading in the list so if you don't need the benefits I mentioned then it may not be a good direction.

In the end, a list or array is really the best fit if you can't see the items changing or being added to.

Upvotes: 1

Tudor
Tudor

Reputation: 62439

I really don't see any problem with using an array. Why is it "obviously not optimal"?

Enums don't seem adequate here. They are generally used when you need to categorize something, not to hold a list describing some concrete items (like folder names). Besides, you cannot use the enum values directly, you need to convert them to strings first.

Upvotes: 0

Oded
Oded

Reputation: 499002

An Enum can't work as you can't have a string based enumeration in C#.

The closest thing to such an enum is a bunch of public constants.

Both options are not great as you can't enumerate over them.

The array option is not bad - not sure what your objection to it is. There is no need to use a for loop however - you can do this:

string[5] list = {"a", "b", "c", "d", "e"};

foreach(string dirName in list){
    // open folder for list[i]
    if (Directory.Exists(string.Format("c:\\{0}", i))){
        // get files and add them to list[i].zip

        // do some work with archive
    }
}

Upvotes: 1

Related Questions