The .Net framework is slightly faster at doing string comparisons between uppercase letters than string comparisons between lowercase letters.
As others have mentioned, some information might be lost when converting from uppercase to lowercase.
You may want to try using a StringComparer
object to do case insensitive comparisons.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
bool isEqualV1 = comparer.Equals("stringA", "stringB");
bool isEqualV2 = (comparer.Compare("stringA", "stringB") == 0);
The .Net Framework as of of 4.7 has a Span
type that should assist in speeding up string comparisons in certain circumstances.
Depending on your use case, you may want to make use of the constructors for HashSet
and Dictionary
types which can take a StringComparer
as an input parameter for the constructor.
I typically use a StringComparer
as an input parameter to a method with a default of StringComparer.OrdingalIgnoreCase
and I try to make use of other techniques (use of HashSets, Dictionaries or Spans) if speed is important.