What is an Associative Array?
๐ก Concept Name
Associative Array โ A data structure that lets you store and retrieve values using custom keys instead of just numbers. Also known as a dictionary or map in most programming languages.
๐ Quick Intro
An associative array lets you find information not by position, but by a special key you choose (like a name or code). In C# and .NET, this powerful idea shows up as Dictionary<TKey, TValue>
.
๐ง Analogy / Short Story
Picture your phoneโs contact list. You never scroll by numberโyou type in the name (the key) and instantly get their number (the value). Associative arrays bring this convenience to programming!
๐ง Technical Explanation
- ๐ Values are stored as key-value pairs, not just in order.
- โก Key lookups are super fastโusually O(1) thanks to hashing.
- ๐ก๏ธ Keys must be unique (no duplicates allowed), but values can repeat.
- ๐ Keys can be any hashable typeโstring, int, even custom objects.
- ๐ ๏ธ Backed by a hash table (or sometimes a balanced tree for sorted maps).
๐ฏ Purpose & Use Case
- โ Lookup tables (country codes, product IDs, settings).
- โ Caching results for fast reuse.
- โ Counting or tallying (like word frequencies).
- โ Storing configurations, user preferences, or feature toggles.
๐ป Real Code Example
// Creating and using a dictionary
var capitals = new Dictionary<string, string> {
{ "USA", "Washington, D.C." },
{ "UK", "London" },
{ "India", "New Delhi" }
};
Console.WriteLine(capitals["India"]); // Output: New Delhi

โ Interview Q&A
Q1: What is an associative array?
A: A structure where each value is linked to a unique key.
Q2: What is the .NET equivalent?
A: Dictionary<TKey, TValue>
in C#.
Q3: Can keys repeat in a dictionary?
A: No, every key must be unique.
Q4: Can values repeat?
A: Yes, values can be anythingโeven duplicated.
Q5: What is the average time complexity for key lookup?
A: O(1), thanks to hash tables.
Q6: Is the order of keys preserved?
A: No, unless you use OrderedDictionary
or SortedDictionary
.
Q7: Can dictionaries grow or shrink as you add/remove entries?
A: Yes, they resize as needed.
Q8: What if you add a duplicate key?
A: It throws an exception at runtime.
Q9: How is a dictionary implemented in .NET?
A: With a hash table under the hood.
Q10: Are string keys case-sensitive in C#?
A: Yes, unless you use a custom comparer.
๐ MCQs
Q1. What does an associative array use for indexing?
- Numbers
- Keys
- Booleans
- Pointers
Q2. Which C# type represents an associative array?
- List
- Array
- Dictionary
- Queue
Q3. What is true about keys in associative arrays?
- They can be duplicated
- They must be unique
- They must be integers
- They must be lowercase
Q4. What is the average time complexity for a key lookup in Dictionary?
- O(1)
- O(n)
- O(log n)
- O(n log n)
Q5. Can values be duplicated in a Dictionary?
- Yes
- No
- Only integers
- Only strings
Q6. Which of the following is NOT an associative array?
- Dictionary
- Map
- Hashtable
- Stack
Q7. Which data structure allows fast access using a key?
- Array
- Stack
- Associative Array
- List
Q8. Which statement is correct about .NET Dictionary?
- It is implemented using arrays
- It uses trees
- It is implemented using hash tables
- It is unordered list
Q9. Can associative arrays grow dynamically?
- No
- Yes
- Only up to 1000 items
- Only during compile time
Q10. What happens if you insert duplicate keys?
- Values are merged
- Both keys are stored
- Exception is thrown
- Older value is kept
๐ก Bonus Insight
Try TryGetValue()
to safely fetch a value without risking an exception if the key isnโt found. For multi-threaded code, use ConcurrentDictionary
for built-in thread safety.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!