Collection Types
Arrays, List<T>, Dictionary<TKey,TValue>, HashSet<T>, queues and the concurrent, immutable and frozen variants. Interviewers ask which collection fits an access pattern.
on this pageshowhide
explore
- Arrays and Lists6 questions
- Dictionaries and Sets6 questions
- Read-Only Interfaces5 questions
- Queues, Stacks and Heaps6 questions
- Immutable and Frozen6 questions
- Thread-Safe Containers6 questions
questions
page 2 of 2Your team publishes a C# library that other teams compile against; which collection types should its public members accept and return, and what does changing one later break?
basics
~20 sAccept the least a member uses, usually IEnumerable<T> or a read-only interface; return what you can keep promising, such as IReadOnlyList<T> or ReadOnlyCollection<T>, never List<T>. Changing a published type later breaks already-compiled callers with MissingMethodException.
In .NET, what do BlockingCollection<T>'s bounded capacity, CompleteAdding and GetConsumingEnumerable each do in a producer–consumer pipeline?
basics
~20 sBlockingCollection<T> wraps a ConcurrentQueue<T> by default. A bounded capacity makes Add block while it is full, Take blocks while it is empty, and CompleteAdding marks the end so GetConsumingEnumerable finishes once the remaining items are taken.
In C#, what does var ready = proofed.And(paid); do to the BitArray named proofed, and when does that call throw?
basics
~10 sBitArray.And writes the result into proofed and returns proofed itself, so ready and proofed are the same object and the original flags are lost. It throws ArgumentException when the two lengths differ.
In .NET, what does LinkedList<T> let you do through LinkedListNode<T> that List<T> cannot, and why is it rarely the faster choice?
basics
~20 sHolding a LinkedListNode<T> lets you insert beside it (AddAfter, AddBefore) or Remove it in O(1) without shifting anything. But finding a node is O(n), there is no indexer, and every item is a separate heap object.
In C#, a level editor takes `CollectionsMarshal.AsSpan(placed)` on a `List<Placement>`, keeps adding entities, and some position writes through the span never appear; why, and what fixes it?
basics
~20 sThe span covers the list's backing array as it was when AsSpan ran. An Add beyond Capacity moves the items to a new array, so later writes silently hit the old one. Take the span after the last Add or Remove.
showing 31–35 of 35