skip to content

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 pageshow

explore

questions

page 2 of 2

Your 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?

level: principalimportance: should knowfreq 32%

basics

~20 s

Accept 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.

open as a page

In .NET, what do BlockingCollection<T>'s bounded capacity, CompleteAdding and GetConsumingEnumerable each do in a producer–consumer pipeline?

level: middleimportance: nice to knowfreq 18%

basics

~20 s

BlockingCollection<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.

open as a page

In C#, what does var ready = proofed.And(paid); do to the BitArray named proofed, and when does that call throw?

level: middleimportance: nice to knowfreq 12%

basics

~10 s

BitArray.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.

open as a page

In .NET, what does LinkedList<T> let you do through LinkedListNode<T> that List<T> cannot, and why is it rarely the faster choice?

level: middleimportance: nice to knowfreq 20%

basics

~20 s

Holding 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.

open as a page

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?

level: seniorimportance: nice to knowfreq 20%

basics

~20 s

The 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.

open as a page

showing 31–35 of 35