What's the most optimal way to see if a string is included in a list of strings? Something that has bugged me for a while, is what way is best to perform a conditional where a string is compared to other strings.  

Here is some sample code.  This code is just for illustration purposes.

The 3 methods I use are


Do a word for word comparison.  This is probably the most optimal, but ugliest code
Create an array on the fly and do a comparison
Have a predefined array.  This is probably better placed in an external file for easier maintainability.


I just wanted to see what your takes are on this, and if there is an even better way to do it.  I know Enums would probably work well here as well.

    private bool IsWordBad(string word)
    {
        //First Method
        if (word == "Foo" || word == "Bar" || word == "FooBar")
        {
            return true;
        }


        //Second Method
        if ((new[] { "Foo", "Bar", "FooBar" }).Contains(word))
        {
            return true;
        }

        //Third Method
        var badWords = new[] { "Foo", "Bar", "FooBar" };
        if (badWords.Contains(word))
        {
            return true;
        }
        return false;
    }
If you really have just three words, then the performance most likely won't matter. In other words, unless profiling shows that this method is performance-critical, use the method that you find most readable.

Some other options you could consider:


Using a switch:

switch (word)
{
    case "Foo":
    case "Bar":
    case "FooBar":
        return true;
    default:
        return false;
}


This has the advantage that it can use compiler optimizations. (Under certain circumstances, the compiler will turn such switch into a Dictionary lookup.)
Use a HashSet&lt;string&gt; instead of an array. Looking up in a hash set is O(1), but O(n) for an array. This could make sense if you had large number of strings to search for.
Use a trie. This has the advantage of comparing only relevant parts of the input string. This could make sense if you had long input strings or if you huge number of strings to search for.