Use Collections Safely

Choose Sharp Runtime collection components and preserve iterator/versioning semantics.

IntermediateTutorial

Choose the family first

NeedComponent
Synchronous lists, dictionaries, sets, queuesCollections.Core
Blocking producer/consumer queueCollections.Blocking
Async enumeration contractsCollections.Async
Observable/object-model collectionsCollections.ObjectModel

Select it

set(SHARP_RUNTIME_COMPONENTS Collections.Core)
target_link_libraries(app PRIVATE SharpRuntime::Collections.Core)

Native rules still apply

  • Mutation invalidates version-sensitive enumerators; do not keep them across changes.
  • Container element ownership follows the chosen C++ value/pointer type.
  • Concurrent collections do not make the objects stored inside them thread-safe.
  • Floating keys need an intentional comparer policy, particularly for NaN and signed zero.
Avoid transliteration by type name alone

A C# collection of managed objects and a C++ collection of values have different copy, identity and lifetime behavior. Decide the element model before selecting the container.