HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpCritical

Comparing two strings, ignoring case in C#

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
twoignoringstringscasecomparing

Problem

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.

Context

Stack Overflow Q#6371150, score: 222

Revisions (0)

No revisions yet.