Which of the following two is more efficient? (Or maybe is there a third option that's better still?)
string val = "AStringValue";
if (val.Equals("astringvalue", StringComparison.InvariantCultureIgnoreCase))
OR
if (val.ToLowerCase() == "astringvalue")
?
If you're looking for efficiency, use this:
string.Equals(val, "astringvalue", StringComparison.OrdinalIgnoreCase)
Ordinal comparisons can be significantly faster than culture-aware comparisons.
ToLowerCase
can be the better option if you're doing a lot of comparisons against the same string, however.
As with any performance optimization: measure it, then decide!