Here is the solution:
class IndexedDictionary<KEY, VALUE> : System.Collections.Generic.Dictionary<KEY, VALUE> {
public VALUE this[int index] { get { return Index[index]; } }
new public void Add(KEY key, VALUE value) {
base.Add(key, value);
RebuildIndex();
}
new public void Remove(KEY key) {
base.Remove(key);
RebuildIndex();
}
new public void Clear() {
base.Clear();
RebuildIndex();
}
void RebuildIndex() {
Index = new VALUE[this.Keys.Count];
if (this.Values.Count > 0)
this.Values.CopyTo(Index, 0);
}
KEY[] Index = new KEY[0];
}
This solution is effective when you read it a log but update less, as
Add/Remove/Clear
is a bit costly. Pay attention for the importance of
new
specifier; it helps to remove the warning. If you want you can modify this code to return
KeyValuePair
or
KEY
.
—SA