Bird
0
0

Identify the error in this C# code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Collections
Identify the error in this C# code snippet:
var list = new List {1, 2, 3};
list = {4, 5, 6};
ACannot assign collection initializer directly to existing list
BList<int> cannot hold integers
CMissing semicolon after first line
DList must be declared with var keyword
Step-by-Step Solution
Solution:
  1. Step 1: Analyze assignment syntax

    Collection initializer syntax can only be used during declaration, not assignment.
  2. Step 2: Identify error cause

    Assigning {4, 5, 6} directly to list causes syntax error.
  3. Final Answer:

    Cannot assign collection initializer directly to existing list -> Option A
  4. Quick Check:

    Collection initializer only valid at declaration [OK]
Quick Trick: Use collection initializer only when declaring new collection [OK]
Common Mistakes:
MISTAKES
  • Trying to reassign collection initializer to existing variable
  • Ignoring syntax rules for collection initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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