Generics: difference between Dictionary, SortedDictionary and SortedList

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

They all seem to be kind of a hashtable and using different algorithms but
what is the actual difference?
When should I use which one?
 
cody said:
They all seem to be kind of a hashtable and using different algorithms but
what is the actual difference?
When should I use which one?

Dictionary is a hashtable, SortedDictionary is a dictionary implemented as a
red-black tree and maintains an order. SortedList appears to be implemented
as a list, using a straight sequential algorithm to locate keys.
 
cody said:
They all seem to be kind of a hashtable and using different algorithms but
what is the actual difference?
When should I use which one?

Oops, forgot to complete that. Each one has different uses, obviously.
Dictionary has no order, so it is appropriate in any circumstance where
order is irrelevent. SortedList would have linear search time, as the last
key in the list will require comparisions to every preceding key, I wouldn't
use it for alot of items if you will be accessing them randomly. I suspect
it would be a good choice if you will access the items in sequence however.
SortedDictionary uses a tree and has a an O(log n) retrieval time, use it
when you will be doing random(key by indexer) retrevials with a fair number
of items, I suspect it won't be as efficent as SortedList when accessing
items in sequence, but I've run no tests to prove that.
 
Back
Top