Difference between Equals/equals and == operator?

前端 未结 11 955
一生所求
一生所求 2020-11-22 14:14

What is the difference between a == b and a.Equals(b)?

11条回答
  •  野性不改
    2020-11-22 14:49

    It depends on the types of a and b.

    In particular, Equals is a virtual method, so that its behavior doesn't depend on the compile-time types of a and b.

    In Java, == will always compare by reference, which is not necessarily what you want, especially for strings.

    In C#, == can be overloaded, but is not virtual (it's a static method). Therefore, if a or b are declared as object, it will compare by reference, even if their actual type overloads operator ==.

    Also, a.Equals(b) will throw a NullReferenceException (NullPointerException in Java) if a is null.

提交回复
热议问题