C# difference between == and Equals()

前端 未结 17 1359
走了就别回头了
走了就别回头了 2020-11-21 06:56

I have a condition in a silverlight application that compares 2 strings, for some reason when I use == it returns false while .Equals()

相关标签:
17条回答
  • 2020-11-21 07:36

    Because the static version of the .Equal method was not mentioned so far, I would like to add this here to summarize and to compare the 3 variations.

    MyString.Equals("Somestring"))          //Method 1
    MyString == "Somestring"                //Method 2
    String.Equals("Somestring", MyString);  //Method 3 (static String.Equals method) - better
    

    where MyString is a variable that comes from somewhere else in the code.

    Background info and to summerize:

    In Java using == to compare strings should not be used. I mention this in case you need to use both languages and also to let you know that using == can also be replaced with something better in C#.

    In C# there's no practical difference for comparing strings using Method 1 or Method 2 as long as both are of type string. However, if one is null, one is of another type (like an integer), or one represents an object that has a different reference, then, as the initial question shows, you may experience that comparing the content for equality may not return what you expect.

    Suggested solution:

    Because using == is not exactly the same as using .Equals when comparing things, you can use the static String.Equals method instead. This way, if the two sides are not the same type you will still compare the content and if one is null, you will avoid the exception.

       bool areEqual = String.Equals("Somestring", MyString);  
    

    It is a little more to write, but in my opinion, safer to use.

    Here is some info copied from Microsoft:

    public static bool Equals (string a, string b);
    

    Parameters

    a String

    The first string to compare, or null.

    b String

    The second string to compare, or null.

    Returns Boolean

    true if the value of a is the same as the value of b; otherwise, false. If both a and b are null, the method returns true.

    0 讨论(0)
  • 2020-11-21 07:37

    The only difference between Equal and == is on object type comparison. in other cases, such as reference types and value types, they are almost the same(either both are bit-wise equality or both are reference equality).

    object: Equals: bit-wise equality ==: reference equality

    string: (equals and == are the same for string, but if one of string changed to object, then comparison result will be different) Equals: bit-wise equality == : bit-wise equality

    See here for more explanation.

    0 讨论(0)
  • 2020-11-21 07:38

    Just as an addition to the already good answers: This behaviour is NOT limited to Strings or comparing different numbertypes. Even if both elements are of type object of the same underlying type. "==" won't work.

    The following screenshot shows the results of comparing two object {int} - values

    0 讨论(0)
  • 2020-11-21 07:38

    Adding one more point to the answer.

    .EqualsTo() method gives you provision to compare against culture and case sensitive.

    0 讨论(0)
  • 2020-11-21 07:40

    ==

    The == operator can be used to compare two variables of any kind, and it simply compares the bits.

    int a = 3;
    byte b = 3;
    if (a == b) { // true }
    

    Note : there are more zeroes on the left side of the int but we don't care about that here.

    int a (00000011) == byte b (00000011)

    Remember == operator cares only about the pattern of the bits in the variable.

    Use == If two references (primitives) refers to the same object on the heap.

    Rules are same whether the variable is a reference or primitive.

    Foo a = new Foo();
    Foo b = new Foo();
    Foo c = a;
    
    if (a == b) { // false }
    if (a == c) { // true }
    if (b == c) { // false }
    

    a == c is true a == b is false

    the bit pattern are the same for a and c, so they are equal using ==.

    Equal():

    Use the equals() method to see if two different objects are equal.

    Such as two different String objects that both represent the characters in "Jane"

    0 讨论(0)
  • 2020-11-21 07:42

    Firstly, there is a difference. For numbers

    > 2 == 2.0
    True
    
    > 2.Equals(2.0)
    False
    

    And for strings

    > string x = null;
    > x == null
    True
    
    > x.Equals(null)
    NullReferenceException
    

    In both cases, == behaves more usefully than .Equals

    0 讨论(0)
提交回复
热议问题