Bird
Raised Fist0

When using LINQ with custom objects, what happens if you try to create a dictionary with duplicate keys using ToDictionary?

hard🧠 Conceptual Q10 of Q15
C Sharp (C#) - LINQ Fundamentals
When using LINQ with custom objects, what happens if you try to create a dictionary with duplicate keys using ToDictionary?
AThe compiler shows an error and code won't compile
BThe dictionary silently overwrites previous entries
CThe duplicate keys are ignored and only unique keys are added
DAn exception is thrown at runtime
Step-by-Step Solution
Solution:
  1. Step 1: Understand ToDictionary behavior

    ToDictionary requires unique keys; duplicates cause errors.
  2. Step 2: Runtime behavior on duplicates

    If duplicate keys exist, ArgumentException is thrown at runtime.
  3. Final Answer:

    An exception is thrown at runtime -> Option D
  4. Quick Check:

    Duplicate keys cause runtime exception in ToDictionary [OK]
Quick Trick: Ensure keys are unique before using ToDictionary [OK]
Common Mistakes:
MISTAKES
  • Expecting silent overwrite
  • Thinking duplicates are ignored
  • Assuming compile-time error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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