Bird
0
0

What happens if you try to add duplicate keys using a Dictionary initializer in C#?

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - Collections
What happens if you try to add duplicate keys using a Dictionary initializer in C#?
AThe last value for the duplicate key overwrites previous ones
BRuntime exception is thrown
CDuplicate keys are ignored silently
DCompile-time error occurs
Step-by-Step Solution
Solution:
  1. Step 1: Understand Dictionary initializer behavior

    Duplicate keys in initializer cause runtime exceptions, not compile errors.
  2. Step 2: Exception type

    ArgumentException is thrown at runtime when duplicate keys are added.
  3. Final Answer:

    Runtime exception is thrown -> Option B
  4. Quick Check:

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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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