Juan
Juan

Reputation: 31

string.Compare behavior

How this can be ? (This is taken from the immediate window in VS2008)

?string.Compare("-", "+")
-1
?string.Compare("-0", "+0")
1

Upvotes: 3

Views: 309

Answers (2)

nes1983
nes1983

Reputation: 15756

The C# manual writes:

The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.

The comparison is performed using word sort rules. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

Upvotes: 1

Joey
Joey

Reputation: 354446

From the remarks on String.Compare (emphasis mine):

The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.

Upvotes: 8

Related Questions