Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 6th, 2012  |  syntax: None  |  size: 1.61 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Whats the difference between using String.Equals(str1,str2) and str1 == str2 [closed]
  2. if(String.Equals(str1, str2))
  3.        
  4. if(str1 == str2)
  5.        
  6. public static bool operator ==(string a, string b)
  7. {
  8.     return Equals(a, b);
  9. }
  10.        
  11. public static bool Equals(string a, string b)
  12. {
  13.     return ((a == b) || (((a != null) && (b != null)) && EqualsHelper(a, b)));
  14. }
  15.        
  16. private static unsafe bool EqualsHelper(string strA, string strB)
  17. {
  18.     int length = strA.Length;
  19.     if (length != strB.Length)
  20.     {
  21.         return false;
  22.     }
  23.     fixed (char* chRef = &strA.m_firstChar)
  24.     {
  25.         fixed (char* chRef2 = &strB.m_firstChar)
  26.         {
  27.             char* chPtr = chRef;
  28.             char* chPtr2 = chRef2;
  29.             while (length >= 10)
  30.             {
  31.                 if ((((*(((int*) chPtr)) != *(((int*) chPtr2))) || (*(((int*) (chPtr + 2))) != *(((int*) (chPtr2 + 2))))) || ((*(((int*) (chPtr + 4))) != *(((int*) (chPtr2 + 4)))) || (*(((int*) (chPtr + 6))) != *(((int*) (chPtr2 + 6)))))) || (*(((int*) (chPtr + 8))) != *(((int*) (chPtr2 + 8)))))
  32.                 {
  33.                     break;
  34.                 }
  35.                 chPtr += 10;
  36.                 chPtr2 += 10;
  37.                 length -= 10;
  38.             }
  39.             while (length > 0)
  40.             {
  41.                 if (*(((int*) chPtr)) != *(((int*) chPtr2)))
  42.                 {
  43.                     break;
  44.                 }
  45.                 chPtr += 2;
  46.                 chPtr2 += 2;
  47.                 length -= 2;
  48.             }
  49.             return (length <= 0);
  50.         }
  51.     }
  52. }
  53.        
  54. IL_0002:  call       bool System.String::Equals(string,
  55.                                               string)