tags:

views:

158

answers:

3

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;
   }

}
+8  A: 

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 IComparable or IComparable<T> when there are semantics for ordering items. Meaning they can be less than, greater than, or equivalent.

LBushkin
Beat me to it :) I've noticed some confusion around the idea of having overloads where the signature of one is just more specific but compatible with another. (e.g. Professor > object)
Rex M
then can i say overloading is compile time polymorphism and overriding is runtime polymorphism?
@generix: Yes you can. Read this: http://wiki.answers.com/Q/Difference_between_runtime_and_compile_time_polymorphism
LBushkin
+1  A: 

The Object.Equals method accepts an object of type 'object' as its parameter. Your Equals method accepts an object of type 'Professor' as its parameter. Both of these methods can co-exist because it is legal to differ two identically-named methods by their parameter list; this is call method overloading.

Erik Forbes
+1  A: 

You don't need to explicitly implement IEquatable if all you want to do is override the default Equals() implementation.

You can just do something like this:

class Professor
{

   public string Name { get; set; }

   public override bool Equals(object cust)
   {
       if (cust == null || !(cust is Professor)) return false;
       return cust.Name == this.Name;
   }

}

Be aware that if you override Equals() you also should override GetHashCode() to ensure proper operation of dictionaries and other collections that make use of hashing to differentiate between objects. Here's the MSDN page guidelines for overriding Equals().

Anna Lear