Dictionary add item as first element instead as last element.

Cheng, Trong-Nghia 1 Reputation point
2022-08-23T14:28:30.383+00:00

Hello,

This code was compiled with .NET Core 6, I couldn't find anything about this "logic" to add my dictionary object new key pair at some point as the first element.

  1. i am using .NET Core 6 2. i use a dictionary-object to add a new key pair, the key is 'datetime' and the value is a double[]'. 3. the first 18 entries, the adding works correctly and the elements were always added as the last element, but the new key pair (date 06.12.2021 00:00:00.001) was added as the first element (see debug monitoring window)

I uploaded the video here https://1drv.ms/v/s!AoltFOAIRGjJr-pvSk4kEm2F77zlow?e=RRCoBw , it happens at second 19.

Is this a strange bug in the SDK? Or is there a logic to it?

With kind regards
Nghia Cheng

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 111.8K Reputation points
    2022-08-23T14:52:20.593+00:00

    Try a SortedDictionary instead of Dictionary. (If you really want to iterate the items in ascending order, not only for debugging).

    I do not think that it is a defect.


  2. Bruce (SqlWork.com) 55,196 Reputation points
    2022-08-23T15:36:21.943+00:00

    the dictionary is implemented as a hash table, so no order is present. if you want a fifo then use a queue. you may want to create a custom class, that stores the items in a hash table, but also keeps a lists for both key lookup and fifo.

    note: many of the collection use a special case for low number of entries, because looping thru a list is faster than key lookup for small number of compares.

    0 comments No comments