Search code examples
c#stringcomparisonequals

Comparing two strings, ignoring case in C#


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")

?


Solution

  • The first one is the correct one, and IMHO the more efficient one, since the second 'solution' instantiates a new string instance.