5

What could possibly be the reasons to use -

bool result = String.Compare(fieldStr, "PIN", true).Equals(0);  

instead of,

bool result = String.Equals(fieldStr, "PIN", StringComparison.CurrentCultureIgnoreCase);

or, even simpler -

bool result = fieldStr.Equals("PIN", StringComparison.CurrentCultureIgnoreCase);

for comparing two strings in .NET with C#?

I've been assigned on a project with a large code-base that has abandon use of the first one for simple equality comparison. I couldn't (not yet) find any reason why those senior guys used that approach, and not something simpler like the second or the third one. Is there any performance issue with Equals (static or instance) method? Or is there any specific benefit with using String.Compare method that even outweighs the processing of an extra operation of the entailing .Equals(0)?

2
  • I disagree with Alex R., and think this should be duped to Differences in string compare methods in C# since that article is very mature and has excellent answers already. Commented Jun 26, 2013 at 5:48
  • 1
    Given links are helpful for anyone to get an understanding of string comparison in C#, i agree, but they by no means reflect my query? Please read the post first. Commented Jun 26, 2013 at 6:05

1 Answer 1

6

I can't give immediate examples, but I suspect there are cases where the first would return true, but the second return false. Two values maybe equal in terms of sort order, while still being distinct even under case-ignoring rules. For example, one culture may decide not to treat accents as important while sorting, but still view two strings differing only in accented characters as unequal. (Or it's possible that the reverse may be true - that two strings may be considered equal, but one comes logically before the other.)

If you're basically interested in the sort order rather than equality, then using Compare makes sense. It also potentially makes sense if the code is going to be translated - e.g. for LINQ - and that overload of Compare is supported but that overload of Equals isn't.

I'll try to come up with an example where they differ. I would certainly say it's rare. EDIT: No luck so far, having tried accents, Eszet, Turkish "I" handling, and different kinds of spaces. That's a long way from saying it cant happen though.

Sign up to request clarification or add additional context in comments.

4 Comments

Jon, that was a bit heavy you know! Except for the LINQ translation possibility part, it took me a while to grasp what you are talking about...whew
@NerotheZero: Cultural differences are like that, unfortunately - full of corner cases and things which seem odd.
Jon, I've edited the question a bit. Could that be anything to add something to your answer?
@NerotheZero: I think you should ask the people who wrote the code, if at all possible - if you're trying to express equality, then I'd use Equals. (And even if you were using Compare, it would me more idiomatic to write == 0 than .Equals(0).)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.