I have a foolish doubt.Generally “System.Object” implements “Equals”. When I implements
IEquatable interface i can give custom definition ( I believe so) to my “Equals”.
so the professor class implementation is equal to
class Professor:System.Object,IEquatable
since there are different definitions of System.Equals ,and IEquatable’s Equals ,Why did not C# report error?.Because I am not overriding “Equals” and even not Hiding “Equals” using new keyword.
class Professor : IEquatable<Professor>
{
public string Name { get; set; }
public bool Equals(Professor cust)
{
if (cust == null) return false;
return cust.Name == this.Name;
}
}
You are neither overriding nor hiding
Object.Equals()because your version takes Professor as a parameter type – not object. Your are overloading the Equals() method.C# allows two methods with the same name to differ on the type of the argument(s) that they accept. This is referred to as overloading – it can be viewed as compile-time polymorphism.
Overriding (which you could, and probably should also do) alters the implementation of a method from its version in a base class. It is the basis for runtime type polymorphism.
Hiding is a less common technique that allows a derived class to mask a version of a method in a base class. Based on the type of the reference through which you make the call, you may either get the base class version (if called through a base class reference) or the derived class version (if called through a derived type reference).
On your second question, you should use
IEquatable<T>when there are semantics for comparing ‘equality’ of two instances that is separate from reference equality.You should implement
IComparableorIComparable<T>when there are semantics for ordering items. Meaning they can be less than, greater than, or equivalent.