Why doesn't Dictionary have AddRange?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Many developers expect Dictionary<TKey, TValue> to have an AddRange method because List<T> has one. The absence is intentional, not an oversight. Bulk insertion for dictionaries is ambiguous because duplicate keys require policy decisions that cannot be universally correct.
Why AddRange Is Straightforward for Lists but Not Dictionaries
List<T>.AddRange simply appends values. Duplicates are allowed, order is preserved, and there is no conflict model.
Dictionaries are different because keys must be unique. During bulk insert, duplicate keys create unavoidable policy questions:
- Throw an error.
- Skip duplicate entries.
- Overwrite existing values.
- Merge values by domain-specific rule.
Each policy is valid in some scenarios and wrong in others. The base API therefore exposes lower-level primitives so callers can choose explicitly.
Existing Dictionary Primitives Already Cover Bulk Scenarios
You can implement any range policy today with Add, indexer assignment, and TryAdd.
That pattern makes conflict handling explicit and observable.
Create Explicit Extension Methods by Policy
If your codebase needs convenience, define policy-specific helpers with clear names.
The method name now documents conflict semantics at call sites.
Merge Policy Example for Aggregation Workloads
Some domains need value merging rather than skip or overwrite.
This is exactly why a single built-in AddRange would be misleading.
Performance Considerations During Bulk Insert
Large dictionary loads can trigger repeated internal resizing if capacity is too small. When approximate size is known, pre-size dictionary.
Pre-sizing improves throughput and reduces allocation churn during large merges.
Also ensure custom key types have stable and efficient GetHashCode implementations. Poor hash quality can degrade expected performance significantly.
API Design Guidance for Teams
Use one clear bulk policy per call path:
- Config ingest usually throws on duplicates.
- Cache warm-up usually overwrites.
- Telemetry aggregation usually merges.
- Data reconciliation often skips and reports duplicates.
Make policy explicit in helper names and metrics so behavior is auditable.
Common Pitfalls
- Implementing a generic
AddRangehelper without naming duplicate-key behavior. - Using exception-based duplicate handling in hot loops.
- Overwriting existing values silently when requirements expected strict uniqueness.
- Ignoring pre-sizing for very large bulk loads.
- Forgetting that custom key equality and hash behavior controls duplicate detection.
Summary
- Dictionary has no built-in
AddRangebecause duplicate-key semantics are context dependent. - Existing primitives already support all bulk insertion policies.
- Policy-specific extension methods are the clearest and safest approach.
- Merge, skip, overwrite, and throw modes should be explicit by use case.
- Pre-sizing and correct key hashing improve bulk-load performance and reliability.

