-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Any class that implements IComparable is *comparable*. That is, you can
compare an instance of that class with another instance in a manner that
you define.
OTOH, a class that implements IComparer is a... well, a comparer (Java
people call this a Comparator), i.e.: a class that imposes ordering on a
set of objects.
When you want to sort an unsorted array of objects of the same class,
you can either make the class implement IComparable, or you can
implement IComparer in a separate class, and pass that to Array.Sort().
Of course if the comparison is done based on a *private* member of the
class, IComparable is more appropriate, since having your IComparator
implementation be able to access its private members may not be
possible/appropriate.
Cybertof wrote:
| Hello,
|
| Could someone please explain me the difference between the 2 interfaces
| IComparable and IComparer ?
|
| In which cases use one or the other ?
| Are they dedicated to own classes or built-in collections ?
|
|
| Regards,
| Cybertof.
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird -
http://enigmail.mozdev.org
iD8DBQE/oPDWwEwccQ4rWPgRAms1AJ9esPvldfXz4pvHKPpiNOiBrYopNACfTnE3
zGByXZQQK28+E1QSoXM8lXE=
=ruXq
-----END PGP SIGNATURE-----