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

atiyar
  • 7,762
  • 6
  • 34
  • 75
  • I disagree with Alex R., and think this should be duped to [Differences in string compare methods in C#](http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp) since that article is very mature and has excellent answers already. – crowder Jun 26 '13 at 05: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. – atiyar Jun 26 '13 at 06:05

1 Answers1

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 – atiyar Jun 26 '13 at 06:14
  • @NerotheZero: Cultural differences are like that, unfortunately - full of corner cases and things which seem odd. – Jon Skeet Jun 26 '13 at 06:16
  • Jon, I've edited the question a bit. Could that be anything to add something to your answer? – atiyar Jun 26 '13 at 06:49
  • @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)`.) – Jon Skeet Jun 26 '13 at 07:10