Bird
0
0

Identify the error in this code snippet using HashSet<int>:

medium📝 Debug Q14 of 15
C Sharp (C#) - Collections
Identify the error in this code snippet using HashSet<int>:
HashSet<int> numbers = new HashSet<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(1);
Console.WriteLine(numbers[0]);
AHashSet does not support indexing with []
BCannot add duplicate values to HashSet
CHashSet must be initialized with values
DAdd method returns void, cannot be used like this
Step-by-Step Solution
Solution:
  1. Step 1: Review HashSet usage

    HashSet stores unique elements but does not support accessing elements by index.
  2. Step 2: Identify invalid operation

    Using numbers[0] causes a compile-time error because HashSet has no indexer.
  3. Final Answer:

    HashSet does not support indexing with [] -> Option A
  4. Quick Check:

    No index access on HashSet [OK]
Quick Trick: HashSet has no indexer; use foreach or Contains [OK]
Common Mistakes:
MISTAKES
  • Trying to access elements by index
  • Thinking Add returns a value
  • Assuming duplicates cause errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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