Difference between InvariantCulture and Ordinal string comparision


  • When comparing two strings in c# for equality, what is the difference between InvariantCulture and Oridinal comparision
    Thursday, January 29, 2009 6:31 PM


  • Ordinal simply does a direct comparison of the Unicode character numbers.  (Note:  Unicode is a superset of ASCII.)  InvariantCulture, on the other hand, knows about special rules.  It represents the rules for a "neutral" culture and for the most part does things in a way that would be considered normal in the United States.  There is also CurrentCulture, which will respect the user's actual region.

    Here is an example where this affects equality:

    Console.WriteLine(String.Equals("æ""ae", StringComparison.Ordinal)); // Prints false  
    Console.WriteLine(String.Equals("æ""ae", StringComparison.InvariantCulture)); // Prints true  

    If your goal is to determine if there is anything different about the two strings, you definitely want to compare for Ordinal equality.  If you want to take into account special language rules, then you will need to go with one of the choices.


    • Marked as answer by liurong luo Monday, February 02, 2009 10:12 AM
    Friday, January 30, 2009 12:04 AM