Edit: In light of a misunderstanding of one of the caveats of StopWatch, my original answer doesn't perform so well as StartsWith in combination with StringComparison.Ordinal. Even if you compile the regex with all the correct options it is a little slower, with performance comparable to using StartsWith without any StringComparison settings. However, the regular expression route does give you more flexibility to match patterns whereas StartsWith doesn't, so I've left my original answer for posterity...
Original Answer:
I have to admit, I'm not sure exactly what you're looking for - however, it seems to me that this kind of code is generally parsing a log file of some description looking to pull out useful information. To save you doing all the string comparisons long hand you could generate a regular expression of the values in your enumeration and then parse the correct enum item using the matched item:
public enum CommandType
{
Acknowledge,
Status,
Echo,
Warning
}
static public CommandType? GetCommandTypeFromString(String command)
{
var CommandTypes = String.Join("|", Enum.GetNames(typeof(CommandType)));
var PassedConstant = Regex.Match(command, String.Format("(?i:^({0}))", CommandTypes)).Value;
if (PassedConstant != String.Empty)
return (CommandType)Enum.Parse(typeof(CommandType), PassedConstant, true);
return null;
}
static void Main(string[] args)
{
Console.WriteLine(GetCommandTypeFromString("Acknowledge that I am great!").ToString());
}
This would pull out CommandType.Acknowledge from the beginning of my string, but only if it existed at the start of the string... it would also pull out the other correct CommandTypes.
Doing similar benchmarking to the accepted answer, I got about a 40% performance increase. I ran the accepted code over a million iterations in 10421ms, but mine ran in only 6459ms.
Of course, while the if statement you're using may not look as efficient as you'd like, it's still easier to read than using the regex form...