0

As the title already says, is insertion order guaranteed by ConditionalWeakTable<TKey, TValue>?

Couldn't find any answers for this on the internet, and it doesn't inherit any other collection class from which it could copy the behavior, so no luck there.

I'm using .NET 6, if that makes any difference for the answer.

Christoph Mett
  • 369
  • 3
  • 16
  • Documentation refers to it as Dictionary so its a hash based storage and those don't normally preserve order. By the way you linked the german documentation and that calls it Wörterbuch and that blurs what its meant here. You obviously do not need the german version so prevent reading that at any cost. Most translation end in things that are just distracting and not actually helpful. – Ralf Nov 04 '22 at 15:19
  • Where did you see, that the documentation refers to it as dictionary? It specifically said 'Although the ConditionalWeakTable class holds a collection of key/value pairs, it is best thought of as a table rather than a dictionary object.' so it is NOT a dictionary. Also ConditionWeakTable is NOT hash based. It works by using ReferenceEquals, not Equals or GetHashcode. So both points not very helpful here. And yes, I fixed the link. I have a chrome extension, that automatically switches the site to English, but sometimes it doesn't change the URL accordingly. But different topic. – Christoph Mett Nov 05 '22 at 01:49
  • From the remarks "A ConditionalWeakTable object is a dictionary that binds a managed object ...". It may not mean implementing IDictionary but its hinting at its internal behavior. While looking at the source i saw that that it uses a hashbased storage pretty likely to the internas of an actual Dictionary just build around Weak references. But its way to complicated to guess concrete behavior. So i was just hinting at the wording of the documentation. – Ralf Nov 05 '22 at 11:22
  • Ah okay, yeah, that makes sense. Yes, I tried diving into the source code, too, but as for you it did not want to reveal its exact logic in regards to keeping or not keeping insertion order. Is there maybe some kind of test one can execute on the ConditionalWeakTable, that could indicate if it preserves its order or not? – Christoph Mett Nov 05 '22 at 15:50
  • Initially you could use the example from the docs. Container.Resize looks problematic. Since the initial Capacity is 8 you might try adding 8 object, iterate the thing, adding a 9th so forcing a resize and iterate again. That might give you a glimps if theres even a chance that order is preserved. That is far from a guarantee and as its not documented it may change anytime. So anything you may find is only valid for the checked framework version. – Ralf Nov 05 '22 at 16:55

1 Answers1

0

Not a perfect answer, because I don't have a technical justification for it, which would be related to the code of the ConditionalWeakTable or something like that, but the class has been in use at our site since I asked the question and by now I can clearly say:

No, ConditionalWeakTable does NOT keep insertion order.

Christoph Mett
  • 369
  • 3
  • 16