Bird
Raised Fist0

Identify the issue in this C# code snippet:

medium📝 Debug Q6 of Q15
C Sharp (C#) - Collections
Identify the issue in this C# code snippet:
HashSet<int> values = new HashSet<int>();
values.Add(10);
values.Add(20);
Console.WriteLine(values[0]);
ACannot add integers to HashSet
BHashSet does not support indexing with []
CHashSet requires initialization with values
DConsole.WriteLine cannot print HashSet elements
Step-by-Step Solution
Solution:
  1. Step 1: Understand HashSet structure

    HashSet<T> is an unordered collection and does not support indexing.
  2. Step 2: Analyze the code

    Attempting to access values[0] causes a compile-time error because HashSet does not have an indexer.
  3. Final Answer:

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

    Use methods like Contains or iterate instead. [OK]
Quick Trick: HashSet has no indexer, use iteration or Contains [OK]
Common Mistakes:
MISTAKES
  • Trying to access elements by index
  • Assuming HashSet is ordered like List
  • Confusing HashSet with arrays or lists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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