5

As a newbie in C# want to know why Object class of C# have two Equals method with below signature.

public virtual bool Equals(object obj);
public static bool Equals(object objA, object objB);

While in Java there is only one equals method.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Satish Pahuja
  • 209
  • 1
  • 6
  • 20
  • @MickyD I read documentation as well but didn't get much difference between these two. – Satish Pahuja Dec 05 '16 at 07:12
  • 3
    http://stackoverflow.com/questions/1451454/c-how-does-the-static-object-equals-check-for-equality - this is not exact duplicate, but is should give you right direction. – Dennis Dec 05 '16 at 07:14
  • Because you might want to compare two values that can be null. You get an exception for `A.Equals(B)` where A is null. So you can use `object.Equals(A, B)` – elshev Dec 05 '16 at 07:14

4 Answers4

11

First

public virtual bool Equals(object obj);

is a standard, typical etc. method to compare objects: if this equals to obj similar to Java's

Second

public static bool Equals(object objA, object objB); 

is a kind of sugar for you not to compare objA with null each time you want to compare objA, objB instances

https://referencesource.microsoft.com/#mscorlib/system/object.cs,f2a579c50b414717

public static bool Equals(Object objA, Object objB) 
{
    if (objA==objB) {
        return true;
    }
    if (objA==null || objB==null) {
        return false;
    }
    return objA.Equals(objB);
}

Java does have a similar method:

https://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#equals(java.lang.Object,%20java.lang.Object)

// Please, note Objects instead of Object
Objects.equals(Object a, Object b);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

The first one compares the current instance with the one provided in the parameter. The later compares A with B as its a static method

matthijsb
  • 909
  • 5
  • 12
0

The first "Equals" method compares the caller object with the arguments.... CallerObject.Equals(targetComparerObject), Moreover, we can also define a custom override for this method in child classes.

The second equals method compares two arguments with each other... :) we cannot override it.

Moreover, in Java the equals methods only compares the memory location. that means when we say obj1.equals(obj2), by default it compares only references, whether they are pointing to same location or not... If we want any custom comparison, we need to override it in JAVA..

  • When you say you can't override the second method... not directly, but it calls the first after performing null comparisons, so it's not like you can't affect its behaviour... – Jon Skeet Dec 05 '16 at 07:15
  • Correct, my statement is true in case when we want to change the behavior before null checks.. – Vijay Vasudevbhai Gurunanee Dec 05 '16 at 07:19
  • But it's misleading in every other case - and as the contract of `Equals` is that it's meant to be symmetric, it's very rare that you *do* want to change how nullity is handled. – Jon Skeet Dec 05 '16 at 07:23
0
public virtual bool Equals(object obj)

This is a object method available in every class object. You use this method to compare current object with the one you passed as a argument.

public static bool Equals(object objA, object objB)

This is a class method available in the Object class which is a top most parent class of all the other classes in C# which means every class in C# is derived from base class Object. It is available as static requiring you to pass two objects as the arguments to get compared.

For more information and examples, you can refer to the documentation.

Object.Equals(obj1, obj2)

https://msdn.microsoft.com/en-us/library/w4hkze5k(v=vs.110).aspx

obj1.Equals(obj2)

https://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx

I don't see any difference between C# and Java in this case. Both do object comparison. It's just about language implementation. C# implemented it in two ways and Java defined it in its own way having it as a object method rather than going for a static one like C#.

Wolverine
  • 1,712
  • 1
  • 15
  • 18