C# Ordered dictionary index

Posted by Martijn on Stack Overflow See other posts from Stack Overflow or by Martijn
Published on 2010-05-18T09:14:05Z Indexed on 2010/05/18 9:20 UTC
Read the original article Hit count: 1655

Filed under:
|
|

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:

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ordereddictionary