Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Why collections over arrays in C Sharp (C#) - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Collections Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of modifying a List vs an Array
What will be the output of this C# code snippet?
C Sharp (C#)
int[] arr = {1, 2, 3};
var list = new List<int>(arr);
list.Add(4);
Console.WriteLine(arr.Length);
Console.WriteLine(list.Count);
A3\n4
B3\n3
C4\n4
D4\n3
Attempts:
2 left
💡 Hint
Remember that arrays have fixed size, but Lists can grow.
🧠 Conceptual
intermediate
1:30remaining
Why choose collections over arrays?
Which of the following is the main reason to prefer collections like List over arrays in C#?
AArrays allow adding elements after creation, collections do not.
BArrays support LINQ queries, collections do not.
CCollections are always faster than arrays.
DCollections can dynamically resize, arrays have fixed size.
Attempts:
2 left
💡 Hint
Think about flexibility in size.
Predict Output
advanced
2:00remaining
Output of modifying collection vs array reference
What will be the output of this C# code?
C Sharp (C#)
int[] arr = {10, 20, 30};
List<int> list = new List<int>(arr);
arr[0] = 100;
list[1] = 200;
Console.WriteLine(arr[0]);
Console.WriteLine(list[1]);
A100\n200
B10\n200
C10\n20
D100\n20
Attempts:
2 left
💡 Hint
Modifying the array changes its own elements; modifying the list changes its own copy.
🔧 Debug
advanced
1:30remaining
Identify the error when resizing an array
What error will this C# code produce?
C Sharp (C#)
int[] arr = new int[3];
arr[3] = 10;
ANullReferenceException
BIndexOutOfRangeException
CSyntaxError
DNo error, runs fine
Attempts:
2 left
💡 Hint
Array indices start at 0 and go up to length-1.
🚀 Application
expert
2:30remaining
Choosing the right data structure for dynamic data
You need to store a list of user names that can grow and shrink during program execution. Which is the best choice in C#?
AUse a string[] array because it is faster to access.
BUse a Dictionary<int, string> to store user names by index.
CUse a List<string> because it can dynamically resize.
DUse a fixed size array and create a new one when resizing is needed.
Attempts:
2 left
💡 Hint
Think about ease of adding and removing elements.

Practice

(1/5)
1. Why might you choose a collection like List<T> over an array in C#?
easy
A. Because collections can change size dynamically while arrays have fixed size.
B. Because arrays have more built-in methods than collections.
C. Because collections use less memory than arrays.
D. Because arrays can store different data types in the same array.

Solution

  1. Step 1: Understand array size behavior

    Arrays in C# have a fixed size once created and cannot grow or shrink.
  2. Step 2: Understand collection size behavior

    Collections like List<T> can dynamically add or remove items, changing their size.
  3. Final Answer:

    Because collections can change size dynamically while arrays have fixed size. -> Option A
  4. Quick Check:

    Collections grow/shrink; arrays fixed size [OK]
Hint: Remember: arrays fixed size, collections flexible size [OK]
Common Mistakes:
  • Thinking arrays can resize automatically
  • Believing collections use less memory always
  • Confusing data type storage capabilities
2. Which of the following is the correct way to declare a collection that can grow in size in C#?
easy
A. int[] numbers = new int[5];
B. List<int> numbers = new List<int>();
C. int numbers = new List<int>();
D. ArrayList numbers = new int[5];

Solution

  1. Step 1: Check syntax for array declaration

    int[] numbers = new int[5]; declares a fixed-size array, not a collection.
  2. Step 2: Check syntax for collection declaration

    List<int> numbers = new List<int>(); correctly declares a generic list collection that can grow.
  3. Final Answer:

    List<int> numbers = new List<int>(); -> Option B
  4. Quick Check:

    List<T> syntax is for growable collections [OK]
Hint: Use List<T> for growable collections, arrays need size upfront [OK]
Common Mistakes:
  • Using array syntax when collection is needed
  • Assigning wrong types to variables
  • Confusing ArrayList with arrays
3. What will be the output of this C# code?
List<int> nums = new List<int>() {1, 2, 3};
nums.Add(4);
Console.WriteLine(nums.Count);
medium
A. Compilation error
B. 3
C. 0
D. 4

Solution

  1. Step 1: Understand initial list size

    The list nums starts with 3 elements: 1, 2, 3.
  2. Step 2: Analyze the Add method effect

    Calling nums.Add(4); adds one more element, increasing count to 4.
  3. Final Answer:

    4 -> Option D
  4. Quick Check:

    List.Count reflects added elements [OK]
Hint: Add increases collection size; Count shows current size [OK]
Common Mistakes:
  • Assuming Count stays 3 after Add
  • Confusing Count with capacity
  • Expecting compilation error due to Add
4. Identify the error in this code snippet:
int[] arr = new int[3];
arr.Add(5);
medium
A. Array size must be declared as 5.
B. int[] cannot store integers.
C. Arrays do not have an Add method.
D. The array must be initialized with values.

Solution

  1. Step 1: Check array methods

    Arrays in C# do not have an Add method; this method belongs to collections like List.
  2. Step 2: Understand array limitations

    Arrays have fixed size and cannot add elements dynamically.
  3. Final Answer:

    Arrays do not have an Add method. -> Option C
  4. Quick Check:

    Arrays lack Add method [OK]
Hint: Arrays fixed size, no Add method; use List for adding [OK]
Common Mistakes:
  • Trying to add elements to arrays
  • Confusing array size with element count
  • Assuming arrays have collection methods
5. You need to store a list of user names that can change during program execution. Which approach is best and why?
hard
A. Use a List<string> because it can grow and shrink as users are added or removed.
B. Use an array because it is faster and fixed size is enough.
C. Use a fixed-size array and recreate it every time the list changes.
D. Use a string variable to store all names separated by commas.

Solution

  1. Step 1: Analyze data size flexibility needs

    User names list changes size, so fixed size arrays are inconvenient.
  2. Step 2: Choose collection type for dynamic data

    List<string> allows adding/removing names easily without recreating the structure.
  3. Final Answer:

    Use a List<string> because it can grow and shrink as users are added or removed. -> Option A
  4. Quick Check:

    Dynamic data needs collections like List [OK]
Hint: Dynamic data? Use List<T> for easy resizing [OK]
Common Mistakes:
  • Choosing fixed arrays for changing data
  • Using strings to store multiple values unsafely
  • Recreating arrays repeatedly instead of collections