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:
List nums = new List();
nums[0] = 10;
Console.WriteLine(nums[0]);
ACannot assign value to index 0 before adding elements.
BList does not support indexing.
CList must be declared with size.
DConsole.WriteLine syntax is incorrect.
Step-by-Step Solution
Solution:
  1. Step 1: Understand List indexing

    List supports indexing but only for existing elements.
  2. Step 2: Check element existence before assignment

    Assigning to index 0 before adding any element causes an error because the list is empty.
  3. Final Answer:

    Cannot assign value to index 0 before adding elements. -> Option A
  4. Quick Check:

    Assigning to empty list index = B [OK]
Quick Trick: Add elements before indexing in List [OK]
Common Mistakes:
MISTAKES
  • Thinking List does not support indexing
  • Assuming List needs fixed size declaration
  • Misreading Console.WriteLine syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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