0

We Have to implement the method of object class => public virtual bool Equals(obj); without using Equals or Reference Equals Method. It has to work same as virtual Equals Method.

I can use objA == objB.

mjwills
  • 23,389
  • 6
  • 40
  • 63
nuli bunny
  • 17
  • 5
  • _"without using ... Reference Equals"_ - why can't you use ReferenceEquals? _"It has to work same as virtual Equals Method."_ - why do you need to implement it then? – ProgrammingLlama Oct 31 '18 at 04:48
  • It is an assignment given to us that ..we should implement a method that works same as virtual Equals Method ..without using Equals or Ref Equals.@John – nuli bunny Oct 31 '18 at 04:53
  • Can you use the `.Equals()` method of an object that you're testing? – ProgrammingLlama Oct 31 '18 at 04:59
  • We should not Equals anywhere in the program@john – nuli bunny Oct 31 '18 at 05:03
  • 1
    Please provide the **full text** of your assessment question, so we have more context. – mjwills Oct 31 '18 at 05:30
  • Assignment : Implement following methods of Object class a. public virtual bool Equals(obj); b. public static bool Equals(objA, objB); c. public static bool ReferenceEquals(objA, objB); Note: Make sure to follow coding standards. @mjwills – nuli bunny Oct 31 '18 at 06:39
  • Where does that say without using Equals or Reference Equals Method? we asked our trainer..so they said we cannot.And,We should implement the methods of object class..not,in object class.@mjwills – nuli bunny Oct 31 '18 at 07:06
  • For Example.I implemented for Reference Equals as:public class ReferenceEqualsClass { public static bool ReferenceEqMethod(Object obj1, Object obj2) { return obj1 == obj2; } } – nuli bunny Oct 31 '18 at 07:09
  • Possible duplicate of [static Equals Method Returning False for Value Types](https://stackoverflow.com/questions/53097503/static-equals-method-returning-false-for-value-types) – mjwills Nov 02 '18 at 04:51

1 Answers1

0

I won't give you a code answer, since this is an assignment after all.

Things you want to check:

  1. Null - are both objects null? Is one object null and the other isn't?

    object.ReferenceEquals(objA, null) is the old preferable way (since it doesn't potentially use an overriden Equals implementation as == would. With C# 7+, you can also use if (objA is null).

  2. You can now compare if (objA == objB). Note that it's here where objA.Equals(objB) would be used, but since that's not allowed, I guess we can use ==.

There is also objA.GetHashCode() which indicates the potential for equality. I say "potential" because it's possible for two very different objects to have the same hash code. If two objects are equal (and correctly implemented) then they should have the same hash code. In short: you can rely on GetHashCode() to indicate the possibility of equality, but you need to do another check (2) to be sure.

See here for more on the relationship between GetHashCode() and Equals(). And see here for more information about == vs Equals()

Community
  • 1
  • 1
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86