C# difference between == and Equals()

前端 未结 17 1401
走了就别回头了
走了就别回头了 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:55

    == Operator

    1. If operands are Value Types and their values are equal, it returns true else false.
    2. If operands are Reference Types with exception of string and both refer to the same instance (same object), it returns true else false.
    3. If operands are string type and their values are equal, it returns true else false.

    .Equals

    1. If operands are Reference Types, it performs Reference Equality that is if both refer to the same instance (same object), it returns true else false.
    2. If Operands are Value Types then unlike == operator it checks for their type first and if their types are same it performs == operator else it returns false.
    0 讨论(0)
  • 2020-11-21 07:56

    There is another dimension to an earlier answer by @BlueMonkMN. The additional dimension is that the answer to the @Drahcir's title question as it is stated also depends on how we arrived at the string value. To illustrate:

    string s1 = "test";
    string s2 = "test";
    string s3 = "test1".Substring(0, 4);
    object s4 = s3;
    string s5 = "te" + "st";
    object s6 = s5;
    Console.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s2), s1 == s2, s1.Equals(s2));
    
    Console.WriteLine("\n  Case1 - A method changes the value:");
    Console.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s3), s1 == s3, s1.Equals(s3));
    Console.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s4), s1 == s4, s1.Equals(s4));
    
    Console.WriteLine("\n  Case2 - Having only literals allows to arrive at a literal:");
    Console.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s5), s1 == s5, s1.Equals(s5));
    Console.WriteLine("{0} {1} {2}", object.ReferenceEquals(s1, s6), s1 == s6, s1.Equals(s6));
    

    The output is:

    True True True
    
      Case1 - A method changes the value:
    False True True
    False False True
    
      Case2 - Having only literals allows to arrive at a literal:
    True True True
    True True True
    
    0 讨论(0)
  • 2020-11-21 07:56

    When we create any object there are two parts to the object one is the content and the other is reference to that content. == compares both content and reference; equals() compares only content

    http://www.codeproject.com/Articles/584128/What-is-the-difference-between-equalsequals-and-Eq

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

    == and .Equals are both dependent upon the behavior defined in the actual type and the actual type at the call site. Both are just methods / operators which can be overridden on any type and given any behavior the author so desires. In my experience, I find it's common for people to implement .Equals on an object but neglect to implement operator ==. This means that .Equals will actually measure the equality of the values while == will measure whether or not they are the same reference.

    When I'm working with a new type whose definition is in flux or writing generic algorithms, I find the best practice is the following

    • If I want to compare references in C#, I use Object.ReferenceEquals directly (not needed in the generic case)
    • If I want to compare values I use EqualityComparer<T>.Default

    In some cases when I feel the usage of == is ambiguous I will explicitly use Object.Reference equals in the code to remove the ambiguity.

    Eric Lippert recently did a blog post on the subject of why there are 2 methods of equality in the CLR. It's worth the read

    • http://blogs.msdn.com/ericlippert/archive/2009/04/09/double-your-dispatch-double-your-fun.aspx
    0 讨论(0)
  • 2020-11-21 07:59

    When == is used on an expression of type object, it'll resolve to System.Object.ReferenceEquals.

    Equals is just a virtual method and behaves as such, so the overridden version will be used (which, for string type compares the contents).

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