views:

244

answers:

4

I'm wondering which collection-type in System.Collections.Generic will support this scenario, if there is one:

Public Class MyCollectionBase(Of ItemType, KeyType)
     Inherits System.Collection.Generic.???

     Default Public Shadows ReadOnly Property Item(ByVal key as KeyType) as ItemType
         Get
             ....
         End Get
     End Property

End Class

In the past, I've used this sort of behavior with Configuration objects inheriting System.Configuration.ConfigurationElementType, like this:

 Default Public Shadows ReadOnly Property Item(ByVal id As String) As Element
     Get
         Return CType(BaseGet(id), Element)
     End Get
 End Property

In this case, though, my collections have nothing to do with configuration, and I need the elements to be generic, as well as the element key/ID.

Thanks in advance.

EDIT: Accepting Joel's answer. I feel like an idiot. For some reason I was glued to the idea that I wanted to stick with an implementation of ICollection that wasn't an IDictionary, probably because I'm refactoring classes that were originally meant to be used with NHibernate mappings. Dictionary is it, of course. I will be hiding in the corner if anyone needs me.

+4  A: 

Dictionary?

Joel Coehoorn
From what I understand of the quetion, ye, dictionary :)
cwap
The wording really isn't very clear :(
Joel Coehoorn
My god, I may have been overthinking this the entire time. I feel like a moron.
AJ
A: 

Looks like a

Dictionary<TKey, ItemType>

is required.

Stevo3000
A: 

It certainly looks like Dictionary<TKey, TValue> is what you want. I would also strongly recommend that you reorder your type parameters - pretty much every dictionary/map/whatever I've ever seen with key and value type parameters has put the key type parameter first.

Jon Skeet
A: 

You're looking for System.Collections.Generic.Dictionary

Adam Robinson