Bird
0
0

What happens if you add duplicate keys in a dictionary initializer like this?

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - Collections
What happens if you add duplicate keys in a dictionary initializer like this?
var dict = new Dictionary<string, int>() { {"a", 1}, {"a", 2} };
ACompilation error due to duplicate keys
BLast value overwrites previous for the key
CRuntime exception thrown
DDictionary stores both entries with same key
Step-by-Step Solution
Solution:
  1. Step 1: Understand dictionary initializer behavior

    Duplicate keys in initializer cause runtime ArgumentException.
  2. Step 2: Analyze options

    Compilation succeeds but runtime throws exception; dictionary cannot have duplicate keys.
  3. Final Answer:

    Runtime exception thrown -> Option C
  4. Quick Check:

    Duplicate keys cause runtime exception [OK]
Quick Trick: Duplicate keys cause runtime error, not overwrite [OK]
Common Mistakes:
MISTAKES
  • Assuming last value silently overwrites
  • Expecting compile-time error
  • Thinking dictionary stores duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes