4

In C#, is String.CompareOrdinal(strA, strB) equivalent to String.Compare(strA, strB, StringComparison.Ordinal)?

I checked the document at https://msdn.microsoft.com/en-us/library/e6883c06.aspx and it doesn't mention this.

Roy
  • 507
  • 10
  • 22
  • 1
    I'm not actually seeing any answers in those dups that actually address this specific question. – juharr May 27 '18 at 11:26

1 Answers1

6

They both do the same thing. You can follow the source from https://referencesource.microsoft.com

  1. public static int CompareOrdinal(String strA, String strB)

  2. Calls private unsafe static int CompareOrdinalHelper(String strA, String strB)

and

  1. public static int Compare(String strA, String strB, StringComparison comparisonType)
  2. Calls private unsafe static int CompareOrdinalHelper(String strA, String strB)

The code paths are nearly identical

In fact the only diffrence is the later has a quick check, so if you have Instruction OCD you can statistically save your self a couple of cycles maybe

   if ((strA.m_firstChar - strB.m_firstChar) != 0)
   {
        return strA.m_firstChar - strB.m_firstChar;
   }
TheGeneral
  • 79,002
  • 9
  • 103
  • 141