PC Review


Reply
Thread Tools Rate Thread

comparer and comparable

 
 
Nick
Guest
Posts: n/a
 
      6th Aug 2003
Why do we have the IComparable and IComparer interfaces don't they do
the same thing (dispite having function which take in eitehr one and
two parameters respectivly). an you give an exaple of where I would
use one and not the other.

Thanks
Nick
 
Reply With Quote
 
 
 
 
Jon Skeet
Guest
Posts: n/a
 
      6th Aug 2003
Nick <(E-Mail Removed)> wrote:
> Why do we have the IComparable and IComparer interfaces don't they do
> the same thing (dispite having function which take in eitehr one and
> two parameters respectivly). an you give an exaple of where I would
> use one and not the other.


They don't do the same thing: IComparable says "I'm willing to be
compared with other things." IComparer says "I'm willing to compare two
things."

For instance, you might use IComparer to compare two strings by length,
whereas string itself implements IComparable to compare strings by
lexical order.

Basically, use IComparable to make it simple to (say) sort objects in
some kind of "default" way, and IComparer to sort objects usually of a
*different* class, in custom ways.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Char Comparer Help Jonathan Wood Microsoft C# .NET 14 16th Jan 2010 08:55 AM
about IComparer and Comparer Tony Microsoft C# .NET 10 17th Jun 2008 11:24 AM
Comparer in generics =?Utf-8?B?TWFyY28gU2VsbGFuaQ==?= Microsoft Dot NET Framework 2 2nd Mar 2006 08:39 AM
Generic Comparer Object Frank Wisniewski Microsoft C# .NET 0 14th Jul 2004 01:53 AM
SortedList - Comparer help sho_nuff Microsoft C# .NET 1 16th Jun 2004 02:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:44 PM.