C # Specified Dictionary Index

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

I use OrderedDictionary because I want to get the object by this identifier, and I want to get the object by its collection index.

I want to use OrderedDictionary as follows:

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

Somewhere in my code, I have something 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. In the last method, I used an index. Imagine _currentIndex is set to 10, but I also have an object with an id of 10. I set Id as the key.

The identifier for MyObject is of type long. This is not true?

+3
source share
1

, OrderedDictionary! object, , int, . , , .

_dict[(object)_currentIndex] as MyObject;
+2

Source: https://habr.com/ru/post/1745946/


All Articles