Bird
0
0

Which of the following is the correct syntax to declare a HashSet<int> and add the number 5 to it?

easy📝 Syntax Q3 of 15
C Sharp (C#) - Collections
Which of the following is the correct syntax to declare a HashSet<int> and add the number 5 to it?
Avar set = new HashSet<int>(); set.Add = 5;
BHashSet<int> set = new HashSet(); set.Add(5);
Cvar set = new HashSet<int>(); set.Add(5);
DHashSet set = new HashSet<int>(); set.Add(5);
Step-by-Step Solution
Solution:
  1. Step 1: Check correct generic HashSet declaration

    var set = new HashSet<int>(); set.Add(5); correctly declares with new HashSet<int>() and uses Add(5).
  2. Step 2: Identify syntax errors in other options

    The option with HashSet<int> set = new HashSet(); misses generic on constructor. The option with set.Add = 5; uses invalid assignment. The option with HashSet set = new HashSet<int>(); has wrong variable type.
  3. Final Answer:

    var set = new HashSet<int>(); set.Add(5); -> Option C
  4. Quick Check:

    Correct HashSet syntax = var set = new HashSet<int>(); set.Add(5); [OK]
Quick Trick: Use Add() method to insert elements into HashSet [OK]
Common Mistakes:
MISTAKES
  • Omitting generic type in constructor
  • Using assignment instead of Add()
  • Declaring variable without generic type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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