Do Not .IndexOf Without Further Checks

tech — tags: , — rohand @ January 26, 2010 11:42 AM

Do not do this:

if ("A,B,C,D,E".IndexOf(key) > -1)
{
    // CODE REDACTED
}

If key is an empty string, the result is 0 and the code in the IF block is executed. This is probably not what you expected.

©2010 appytizers. All rights reserved.