7

Possible Duplicate:
C# difference between == and .Equals()

In my daily code routine I use them a lot, but really don't know how exactly they are different from each other.

if(String.Equals(str1, str2))

and

if(str1 == str2)
Community
  • 1
  • 1
Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
  • 5
    Please don't close. This is **not a duplicate** of difference between `==` and `object.Equals` -- although that is a good link. Both `string.Equals(a,b)` and `string.==` are *not* polymorphic, which is the big difference between `==` and `object.Equals`. –  Jan 06 '11 at 22:48
  • voting to close, these two questions can clearly be merged. – Woot4Moo Jan 06 '11 at 22:50
  • @Woot4Moo I disagree. This is a *specific* question about two *specific* ways of doing a *string comparison*. The "duplicate question" talks about `==`/`object.Equals` in *general*. Perhaps `string.Equals` could also be mentioned, but this would be lost in a merge. –  Jan 06 '11 at 22:52
  • @pst that's fine to disagree, but if you read the other question that is proposed by ChrisF you can see that it clearly illustrates the difference. Therefore merging or deletion of this duplicate would work. – Woot4Moo Jan 06 '11 at 22:53
  • @Woot4Moo I read the other question, and the responses. None are a satisfactory answer which covers [`static bool string.Equals(string,string)`](http://msdn.microsoft.com/en-us/library/1hkt4325.aspx) -- this method is not the same as [`string.Equals(string)`](http://msdn.microsoft.com/en-us/library/858x0yyx.aspx). Since the other question is *general*, and this is *specific*, a merge/close is inadvisable in my mind. –  Jan 06 '11 at 22:54
  • If you read the responses `pst` you would see that `BlueMonk` and `Merhdad` both have the pieces to answer the question. – Woot4Moo Jan 06 '11 at 22:56
  • 1
    @Woot4Moo Please read my last comment. BlueMonk and Merhdad are talking about the *non-static* [`string.Equals`](http://msdn.microsoft.com/en-us/library/858x0yyx.aspx) method. This question is about the *static* [`string.Equals`](http://msdn.microsoft.com/en-us/library/1hkt4325.aspx) method. –  Jan 06 '11 at 22:58
  • @pst if this is about the static version the OP didn't do a good job explaining it. – Woot4Moo Jan 06 '11 at 22:59
  • 1
    @Woot4Moo Please note the question contains `String.Equals(str1, str2)` (perhaps better as `string.Equals(str1, str2)`) and not `str1.Equals(str2)`. –  Jan 06 '11 at 23:00

2 Answers2

6

(UPDATE)

They are in fact exactly the same.

public static bool operator ==(string a, string b)
{
    return Equals(a, b);
}

so == calls the Equals.


public static bool Equals(string a, string b)
{
    return ((a == b) || (((a != null) && (b != null)) && EqualsHelper(a, b)));
}

EqualsHelper is an unsafe method:

UPDATE What it does, it loops through the characters using integer pointers and compares them as integers (4byte at a time). It does it 10 at a time and then one at a time.

private static unsafe bool EqualsHelper(string strA, string strB)
{
    int length = strA.Length;
    if (length != strB.Length)
    {
        return false;
    }
    fixed (char* chRef = &strA.m_firstChar)
    {
        fixed (char* chRef2 = &strB.m_firstChar)
        {
            char* chPtr = chRef;
            char* chPtr2 = chRef2;
            while (length >= 10)
            {
                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)))))
                {
                    break;
                }
                chPtr += 10;
                chPtr2 += 10;
                length -= 10;
            }
            while (length > 0)
            {
                if (*(((int*) chPtr)) != *(((int*) chPtr2)))
                {
                    break;
                }
                chPtr += 2;
                chPtr2 += 2;
                length -= 2;
            }
            return (length <= 0);
        }
    }
}
Aliostad
  • 80,612
  • 21
  • 160
  • 208
2

They are absolutely the same. Here is what ildasm shows for ==

  IL_0002:  call       bool System.String::Equals(string,
                                              string)

Also read the documentation: http://msdn.microsoft.com/en-us/library/system.string.op_equality.aspx It says

This operator is implemented using the Equals method

Stilgar
  • 22,354
  • 14
  • 64
  • 101
  • 1
    intermediate language disassembler - a disassembler for .net byte-code – CodesInChaos Jan 06 '11 at 23:07
  • 2
    ildasm is the tool that shows the IL code for a .NET assembly. It is part of the SDK and its location depends on the actual version of the framework. For example C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin – Stilgar Jan 06 '11 at 23:08