2

I was wondering why is Object.Equals(Object obj) virtual. I understand that we can override that method and write our own code for checking equality instead of the base Object.Equals(Object obj) method that checks only reference equality.

What I say is why override it when i can implement my own new method in my defined type? Is there any specific reason?

  • 1
    simple answer, everything in C# is derived from Object class. – Vivek Nuna Jan 20 '17 at 12:09
  • The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class. The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into derived class. – Vivek Nuna Jan 20 '17 at 12:11
  • 1
    @viveknuna Simple but incorrect, not everything is derived from `Object`, unless I am mistaken. – TheLethalCoder Jan 20 '17 at 12:12
  • 1
    Why would you want to create your own method to do the same thing? You wouldn't want a `Maths` class with an `Add` method then a derived class uses `Add2` because it changes the functionality slightly. You would expect a single `Add` method. – TheLethalCoder Jan 20 '17 at 12:13
  • Consider `Object o = new DerivedClassFromObject();` calling o.Equals with virtual/override will call `DerivedClassFromObject.Equals` without virtual, but new operator, it will call `Object.Equals`. See http://stackoverflow.com/a/3838692/982149 – Fildor Jan 20 '17 at 12:14
  • Possible duplicate of [Overloading and overriding](http://stackoverflow.com/questions/673721/overloading-and-overriding) – Fildor Jan 20 '17 at 12:18
  • Why override? Because it's OOP. – Renatas M. Jan 20 '17 at 12:19
  • I would say that it is mainly historical since generics were added in C# 2 so virtual Equals method was needed for non generics containers. – Phil1970 Jan 20 '17 at 14:08

2 Answers2

1

You would override it for the same reason you would want to override any method rather than hiding it with a new method in a derived class: because of polymorphism. You don't know how your derived class is going to be used by other code which only might know about the base class.

Clients may not even know that you've overridden the class at all, but they do know that they can call Equals on your instances because everything derives from Object. If you have some other, new method, the code using your instances will not know to call that method instead. It's the Liskov Substitution Principle at work.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
1

What I say is why override it when i can implement my own new method in my defined type?

Because that is how the language feature has been designed, as you know every type in C# is inheriting from Object and Object defines the methods with default implementation to check equality of two objects, and we as developers creating new types might want to modify the behavior how equals method compare two objects of specific type.

Here is an example to understand why it is virtual:

int i = 1;
int j = 1;
Object o = i;
o.Equals(j);  // now what will happen here if you have'nt overriden it

the int type contains the overriden implementation of Equals method which checks the two integers for equality, so when we will call Equals method using reference of type Object it will call the implementation defined in the type System.Int32,and we had a new method defined in the System.Int32 and not override the Equals method of Object, then we would see unexpected behavior as Object type implementation would have checked the memory addresses i.e reference equality.

Consider this example as well:

public class Person  
   {  

       private string _name;  

       public string Name   
       {   
           get   
           {   
               return _name;   
           }   
       }  

       public Person(string name)  
       {  
           _name = name;  
       }  

       public override string ToString()  
       {  
           return _name;  
       }  

   }  

What if i want Equals method to not compare them by reference but instead we want to compare them on basis of name, in that case we would need to override the Equals method so that calling it either with Object type reference or Person type reference it would return the same result i.e not checking reference equality but checking the Name of two Person class objects.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160