Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between using String.Equals(str1,str2) and str1 == str2 [duplicate]

Tags:

string

c#

equals

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)
like image 584
Rami Alshareef Avatar asked Jan 06 '11 22:01

Rami Alshareef


People also ask

What is the difference between Equals () method and == operator?

equals() method. The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.

What is the difference between string Equals and == in Java?

== is an operator and equals() is a method. Operators are generally used for primitive type comparisons and thus == is used for memory address comparison and equals() method is used for comparing objects.

What is the difference between == and === in C#?

Difference between == and Equals() Method in C# In C#, the equality operator == checks whether two operands are equal or not, and the Object. Equals() method checks whether the two object instances are equal or not.

Which is better Equals or == in C#?

The Equality Operator ( ==) is the comparison operator and the Equals() method compares the contents of a string. The == Operator compares the reference identity while the Equals() method compares only contents.


2 Answers

(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);
        }
    }
}
like image 118
Aliostad Avatar answered Oct 12 '22 23:10

Aliostad


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

like image 38
Stilgar Avatar answered Oct 12 '22 22:10

Stilgar