Question

I have a use-case for a string collection that never changes in the life of the application and will not likely to change even between application versions.

The only operation I would like to do on it (apart from populating it) is checking if it contains a given string.

The list needs to be accessable from every thread.

I needed this fast so I had hastily created this:

private static readonly System.Collections.Concurrent.BlockingCollection<string> coll = new System.Collections.Concurrent.BlockingCollection<string>();

static WysiwygController()
{
    coll.Add("aaa");
    coll.Add("bbb");
    coll.Add("ccc");
    coll.Add("ddd");
}

And checked if it contains a string by:

foreach (var item in coll)
{
    if (item == something)
    {
        return true;
    }
}
return false;

While it works I highly doubt that I have picked the most efficient collection for this particular task.

Is there a collection type more suitable for this?

Était-ce utile?

La solution

Since you are only Reading from list you could really use any number of collections (i.e., List, HashSet, etc).

With regards to checking if the list Contains an item. I would recommend using the Contains method available from any IEnumerable collection type. Or... You could use a Linq query...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top