1

According To MSDN : StringComparison.InvariantCulture :

Specifies the culture, case, and sort rules to be used by certain overloads of the String.Compare and String.Equals methods.

Well , I'm not doing any sort here in my sample , And still don't understand why does it yield the result it yield :

/*1*/   void Main()
/*2*/   {
/*3*/        string s1 = "lasst";
/*4*/        string s2 = "laßt"; 
/*5*/        Console.WriteLine (s1.Equals(s2, StringComparison.InvariantCulture));  
/*6*/        //True
/*7*/        
/*8*/        
/*9*/        
/*10*/       string s3 = "hello";
/*11*/       string s4 = "héllo"; 
/*12*/       Console.WriteLine (s3.Equals(s4, StringComparison.InvariantCulture)); 
/*13*/       //False
/*14*/   }

InvariantCulture uses comparison rules based on english, but without any regional variations

1) So why it says that lasst is equal to laßt ? (one doesnt even has an english char...)

2) And why ( if it's flatten to english)hello is not equal to héllo ?

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • It's not "flattened to English" - it's just that the rules are *based* on English. I think for more details you should probably look at unicode.org, which is likely to go into this in a *lot* of detail. – Jon Skeet Jul 30 '13 at 10:58
  • @JonSkeet Jon, But `ß` is no an english char so how can it be based on english? – Royi Namir Jul 30 '13 at 11:00
  • "Based on" != "exactly the same as". – Jon Skeet Jul 30 '13 at 11:00

1 Answers1

5

Relevant snippet from the book CLR via C#

Note When the Compare method is not performing an ordinal comparison, it performs character expansions. A character expansion is when a character is expanded to multiple characters regardless of culture. In the above case, the German Eszet character ‘ß’ is always expanded to ‘ss.’ Similarly, the ‘Æ’ ligature character is always expanded to ‘AE.’ So in the code example, the second call to Compare will always return 0 regardless of which culture I actually pass in to it.

You string "héllo" does not get transformed by character expansion internally, and so is not considered equal to "hello".

Chris McAtackney
  • 5,192
  • 8
  • 45
  • 69
  • thanks Let me see if i got it : the `ß` char sounds like `ss` so there is an internal rule which expand is to `ss` and that's why its equal. and `é` does NOT has a rule which expand it ( it is not expandable---- p.s. why ? becuase it doesnt sounds the same ?) and thats why it is different ? – Royi Namir Jul 30 '13 at 11:08
  • 1
    _sounds like `ss` so there is an internal rule_ - of course not, this rule is deeply entrenched in German custom. – H H Jul 30 '13 at 11:53
  • @HenkHolterman I dont understnad - What part exactly in .net indicates that "‘ß’ is always expanded to ‘ss`." ? – Royi Namir Jul 30 '13 at 11:57
  • I think Chris just quoted it here. Pity there is no link (yet). – H H Jul 30 '13 at 12:11
  • @Royi Namir, you'll have to ask a German speaker about the equivalence of "ß" and "ss", as I only have the text quoted above to base my answer off unfortunately. I tried drilling down through the CompareTo method to see if I could find where this character expansion is happening, but couldn't see it before I ran into a native call to InternalCompareString. – Chris McAtackney Jul 30 '13 at 12:42