Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello people ,


Why do i need to use an IComparable interface to compare types. Because finally we will be implementing its CompareTo() in the class which will inherit this interface.

Instead of this i can use method overloading in the same class so as to compare different types.


Thanks
Posted

1 solution

Because overloading the equals and not equals operators does not do the same thing: Anything which implements IComparable is allowed to use the built in Sorting and Ordering functions, which is not available to those that don't.

The reason for this is pretty simple: unless you implement the IComparable interface the system does not know that your class, and all the classes derived from your class, implement the methods it needs to actually establish an order of the elements of a collection.

Think of it this way: when you apply for a job, you can be as good at C# as possible, but unless you have the qualifications to prove that you are "in the club" your CV is likely to go in the bin, because the employer cannot verify that you know anything about it. The qualification ensures that a minimum standard has been met and that it is worth interviewing you. In other words, by implementing the IQualified interface, you automatically prove that you are capable of doing the job.
 
Share this answer
 
Comments
Rahul VB 21-Aug-13 14:10pm    
Hello Sir,

Thats a fantastic answer thanks a lot. Similarly please give me an explanation of Idisposable interface also thanks a lot. I am being a little greedy hehe.
Rahul VB 21-Aug-13 14:15pm    
Hello Sir,

This real world example which you gave is fantastic. Unless i inherit the qualification interface i wont be able to implement my job method ( get a job). I hope i am correct. So please do also explain me Idisposable method in the same way. I am sorry i am being greedy hehe, but thanks again.

Thanks again
OriginalGriff 21-Aug-13 14:26pm    
I tell you what, why don't you explain IDisposable to me? That way, I think you will learn more as you have to think about what IDisposable actually does - and it's an important one, a lot more so than IComparable.

(And this is sounding a lot like your homework :laugh:)
Rahul VB 2-Jan-14 15:23pm    
Respected Sir,
After this comment i became your fan :laugh:)
Member 10070204 is me.

Thanks,
Rahul
Rahul VB 24-Aug-13 15:06pm    
Hello Sir,
I am back with my homework and sorry for the delay,

Normally we have garbage collector in order to free down managed resources when that particular object is no longer used. It cant be predicted as to when and how the garbage collector will come into picture.

However if i want to free unmanaged resources like a window handle or some thing like that, then i can forcefully release the object and free the resource if i implement the dispose method of the IDisposable interface.

Suppose my homework is finished and my teacher signs my notebook then only i can say that i have finished my home work. So i can say that i have implemented the Ihomework interface's finished() method forcefully. Forcefully because only then will my teacher award me with points.


I hope i am correct.

:)
Thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900