IComparable接口的重要性是什么? [英] What is the Importance of IComparable interface ?

查看:242
本文介绍了IComparable接口的重要性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,





为什么我需要使用IComparable接口来比较类型。因为最后我们将在继承此接口的类中实现其CompareTo()。



而不是这个我可以在同一个类中使用方法重载以便比较不同的类型。





谢谢

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

推荐答案

因为重载等于而不是equals运算符不会做同样的事情:任何实现IComparable的东西都允许使用内置的Sorting和Ordering函数,而那些没有的函数则无法使用。



原因很简单:除非你实现了IComparable接口,否则系统不知道你的类和从你的类派生的所有类实现了它需要实际建立一个集合元素的顺序。



想一想:当你申请工作时,你可以擅长C#尽可能,但除非你有资格证明你是在俱乐部,你的简历很可能会进入垃圾箱,因为雇主无法验证你对它有任何了解。资格证书确保符合最低标准,值得面试。换句话说,通过实现IQualified接口,您将自动证明您有能力完成这项工作。
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.


这篇关于IComparable接口的重要性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆