Thanks, but that is too much overhead for what I am trying to do.
I went ahead and just wrote a custom collection that used both the IList<>
and IDictionary<> interfaces:
public class SwitchCollection<K, T> : IList<T>, IDictionary<string, T>
where K : IComparable
where T : SwitchInterface
{
// stored in two collections
List<T> _internalList = new List<T> ();
Dictionary<string, T> _internalDictionary = new Dictionary<string, T> ();
..
..
..
public T this[ int i ]
{
get
{
return _internalList[ i ];
}
set
{
if ( _internalDictionary.ContainsKey( value.Name ) == false )
throw new IndexOutOfRangeException ();
// Set the list
_internalList[ i ] = value;
// set the dictionary
_internalDictionary[ value.Name ] = value;
}
}
public T this[ string key ]
{
get
{
return _internalDictionary[ key ];
}
set
{
// Find in list
bool found = false;
int i = this.IndexOf ( value );
// Set the list
_internalList[ i ] = value;
// set the dictionary
_internalDictionary[ value.Name ] = value;
}
}
..
..
..
}
"Peter Sestoft" wrote:
gmccallum <gm*******@discussions.microsoft.com> writes:
I am trying to create a base class that contains a collection that MUST be
able to be referenced by both a string key and a positional index. This
collection MUST keep the items in the same order as they are added.
In 2.0, is there a collection that would support this?
The List<> works to keep the order but doesn't allow me to reference the
items by key like the Dictionary<> collection does, but I don't think the
Dictionary collection is guarenteed to keep the items in order.
Use HashedLinkedList<String> or HashedArrayList<String> from the C5
generic collection library at http://www.itu.dk/research/c5/
Peter