开发者

C# Ordered dictionary index

I am considering of using the OrderedDictionary. As a key I want to use a long value (id) and the value will be a custom object.

I use the OrderedDictionary because I want to get an object by it's Id and I want to get an object by it's 'collection' index.

I want to use the OrderedDictionary like this:

public void AddObject(MyObject obj)
{
  _dict.Add(obj.Id, obj); // dict is declared as OrderedDictionary _dict = new OrderedDictionary();
}

Somewhere else in my code I have something similar like this:

开发者_StackOverflow
public MyObject GetNextObject()
{
  /* In my code keep track of the current index */

  _currentIndex++;
  // check _currentindex doesn't exceed the _questions bounds
  return _dict[_currentIndex] as MyObject;
}

Now my question is. In the last method I've used an index. Imagine _currentIndex is set to 10, but I have also an object with an id of 10. I've set the Id as a key.

The Id of MyObject is of type long?. Does this goes wrong?


Updated as I never noticed the OrderedDictionary part! The indexer has an override of either object which will retrieve the value by key, or int which will retrieve the value by index. You would need to cast your index as an object if you are looking to retrieve it by key e.g.

_dict[(object)_currentIndex] as MyObject;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜