0

In the exact setup:

namespace NS
{
    class Program
    {
        static void Main(string[] args)
        {
            Object obj1 = new A();
            Object obj2 = new A();
            Console.WriteLine(obj1.GetType());
            Console.WriteLine(obj2.GetType());

            Console.WriteLine(obj1.Equals(obj2)); // why not entering A::Equals(A a)
            Console.ReadLine();
        }
    }
    class A
    {
        int x;
        public bool Equals(A a)
        {
            Console.WriteLine("enters Equals");
            return this.x == a.x;
        }
    }
}

I have the output from console app in C#:

NS.A
NS.A
False

Question: If ojb1 and obj1 are of type NS.A why public bool Equals(A a) is not entered?

Is Console.WriteLine(obj1.GetType()); "lies" about a real object type. I am confused?

I wanted to call A.Equals(A) method from code even though Object obj1 = new A();

I know I can fix the problem with A obj1 = new A(); but I am stuck with understanding why GetType() returns the A and I cannot call the A.Equals(A).

Good Luck
  • 113
  • 6
  • You're calling `Object.Equals(Object)` in your code, not `A.Equals(A)`. Did you mean to override the `Object` method? https://stackoverflow.com/questions/9317582/correct-way-to-override-equals-and-gethashcode – Joe Sewell Jan 12 '22 at 21:40
  • Yes. this was the idea. – Good Luck Jan 12 '22 at 21:41

1 Answers1

1

You need to let it know it is overriding and not creating a new function.

    public override bool Equals(object a)
    {
        Console.WriteLine("enters Equals");
        return ((A)this).x == ((A)a).x;
    } 

Your code is creating two objects obj1 and obj2. While asking for the object type, it is returning the type A as that is the type of obj1 and obj2. However you are containing obj1 and obj2 in an object variable. So when you call obj1.Equals it is calling Object's Equals method. You can override this with the override command and taking the same parameters in, or you can explicitly tell your program you want it to use the (A) class method.

    class Program
    {
        static void Main(string[] args)
        {
            Object obj1 = new A();
            Object obj2 = new A();
            Console.WriteLine(obj1.GetType());
            Console.WriteLine(obj2.GetType());

            Console.WriteLine(obj1.Equals(obj2)); // why not entering A::Equals(A a)
            
            Console.WriteLine(((A)obj1).Equals((A)obj2));
            A a1 = new A();
            A a2 = new A();
            
            Console.WriteLine(a1.Equals(a2));
            
            Console.ReadLine();
        }
    }
    class A
    {
        int x;
        public bool Equals(A a)
        {
            Console.WriteLine("enters Equals");
            return ((A)this).x == ((A)a).x;
        }
    }

returns

A
A
False
enters Equals
True
enters Equals
True

Here is more information about overriding.

Cheers!

Daniel Lord
  • 754
  • 5
  • 18
  • This really solved the problem. Why am I missing this concept? Actually the original problem I am solving had `public new bool Equals(object a)` and I was shocked that the compiler swallowed that. Can you elaborate since when methods can be `new`? – Good Luck Jan 12 '22 at 21:51
  • 1
    _the `new` modifier **hides** an accessible base class method_ - (emphasis added) - from Knowing When to Use Override and New Keywords (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords) – stuartd Jan 12 '22 at 22:05