Bird
0
0

What happens if you try to add a duplicate element to a HashSet<int> in C#?

easy🧠 Conceptual Q1 of 15
C Sharp (C#) - Collections
What happens if you try to add a duplicate element to a HashSet<int> in C#?
AThe duplicate element replaces the existing one.
BThe duplicate element is ignored and not added.
CAn exception is thrown at runtime.
DThe HashSet clears all elements before adding the duplicate.
Step-by-Step Solution
Solution:
  1. Step 1: Understand HashSet behavior on duplicates

    A HashSet stores unique elements and ignores duplicates when adding.
  2. Step 2: Check what happens on adding a duplicate

    Adding a duplicate element does not change the set and no error occurs.
  3. Final Answer:

    The duplicate element is ignored and not added. -> Option B
  4. Quick Check:

    HashSet duplicate add = ignored [OK]
Quick Trick: HashSet ignores duplicates silently when adding [OK]
Common Mistakes:
MISTAKES
  • Thinking duplicates replace existing elements
  • Expecting an exception on duplicates
  • Assuming HashSet clears before adding duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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