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
Concept Flow - List generic collection
Create List<T> instance
↓
Add elements to List
↓
Access elements by index
↓
Modify elements
↓
Remove elements
↓
Iterate over List
↓
Use List methods (Count, Clear, Contains)
This flow shows how a List<T> is created, elements are added, accessed, modified, removed, and iterated with built-in methods.
Execution Sample
C Sharp (C#)
List<int> numbers = newList<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
int first = numbers[0];
numbers[1] = 25;
numbers.Remove(30);
This code creates a list of integers, adds three numbers, accesses the first element, modifies the second, and removes the number 30.
Execution Table
Step
Operation
List Content
Index Access/Modification
Result/Effect
1
Create empty List<int>
[]
-
List initialized with 0 elements
2
Add 10
[10]
-
10 added at index 0
3
Add 20
[10, 20]
-
20 added at index 1
4
Add 30
[10, 20, 30]
-
30 added at index 2
5
Access first element
[10, 20, 30]
numbers[0]
Returns 10
6
Modify second element
[10, 25, 30]
numbers[1] = 25
20 changed to 25
7
Remove element 30
[10, 25]
-
30 removed from list
💡 All operations complete; list now contains [10, 25]
Variable Tracker
Variable
Start
After Step 2
After Step 3
After Step 4
After Step 5
After Step 6
After Step 7
numbers
[]
[10]
[10, 20]
[10, 20, 30]
[10, 20, 30]
[10, 25, 30]
[10, 25]
first
undefined
undefined
undefined
undefined
10
10
10
Key Moments - 3 Insights
Why does numbers[1] = 25 change the list element instead of adding a new one?
Because accessing by index with assignment replaces the existing element at that position, it does not add a new element. See step 6 in execution_table where the list changes from [10, 20, 30] to [10, 25, 30].
What happens if we try to access an index that does not exist?
Accessing an index outside the list range causes an error. In the example, we only access numbers[0] which exists. Trying numbers[3] would cause an exception.
How does Remove(30) know which element to remove?
Remove searches the list for the first occurrence of the value 30 and removes it. In step 7, it removes the last element 30, changing the list to [10, 25].
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what value does numbers[0] return?
A10
B20
C25
D30
💡 Hint
Refer to step 5 in execution_table where numbers[0] is accessed and returns 10.
At which step does the list change from [10, 20, 30] to [10, 25, 30]?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Check step 6 in execution_table where numbers[1] is modified to 25.
If we add numbers.Add(40) after step 7, what will the list content be?
A[10, 25]
B[10, 25, 40]
C[40, 10, 25]
D[10, 25, 30, 40]
💡 Hint
After step 7, list is [10, 25]. Adding 40 appends it at the end.
Concept Snapshot
List<T> is a resizable array.
Use Add() to append elements.
Access elements by index (zero-based).
Modify elements by assigning to index.
Remove elements by value or index.
Use Count to get number of elements.
Full Transcript
This visual trace shows how a List<int> is created and manipulated step-by-step. We start with an empty list, add three numbers 10, 20, and 30. Then we access the first element which is 10. Next, we modify the second element from 20 to 25. Finally, we remove the element 30. The variable tracker shows how the list content changes after each operation. Key moments clarify common confusions like modifying vs adding elements and how Remove works. The quiz tests understanding of list content at different steps and effects of operations.
Practice
(1/5)
1. What is the main feature of a List<T> in C#?
easy
A. It can only hold a fixed number of items.
B. It stores only unique items and does not allow duplicates.
C. It automatically sorts items when added.
D. It stores items in order and allows easy access by position.
Solution
Step 1: Understand List<T> behavior
A List<T> stores items in the order they are added and allows access by index.
Step 2: Compare options with List<T> features
Only It stores items in order and allows easy access by position. correctly describes this behavior; others describe different collection types or incorrect features.
Final Answer:
It stores items in order and allows easy access by position. -> Option D
Quick Check:
List<T> = ordered, indexed collection [OK]
Hint: Remember List<T> keeps order and supports indexing [OK]
Common Mistakes:
Thinking List<T> enforces uniqueness
Assuming List<T> auto-sorts items
Believing List<T> has fixed size
2. Which of the following is the correct way to declare a List of integers in C#?
easy
A. List<int> numbers = new List<int>();
B. List numbers = new List<int>();
C. List<int> numbers = List<int>();
D. List<int> numbers = new List();
Solution
Step 1: Recall correct List<T> syntax
In C#, to declare a generic List, you must specify the type and use the new keyword with constructor.
Step 2: Check each option for syntax correctness
List<int> numbers = new List<int>(); correctly declares and initializes a List of int. Others miss type, constructor, or use wrong syntax.
Final Answer:
List<int> numbers = new List<int>(); -> Option A
Quick Check:
Generic List declaration = new List<T>() [OK]
Hint: Use new List<T>() with type specified [OK]
Common Mistakes:
Omitting new keyword
Not specifying generic type in constructor
Using non-generic List without type
3. What will be the output of this C# code?
var fruits = new List<string> { "apple", "banana", "cherry" };
fruits.RemoveAt(1);
Console.WriteLine(fruits[1]);
medium
A. banana
B. IndexOutOfRangeException
C. cherry
D. apple
Solution
Step 1: Understand RemoveAt effect on list
RemoveAt(1) removes the item at index 1, which is "banana". The list becomes ["apple", "cherry"].
Step 2: Access the item at index 1 after removal
After removal, fruits[1] is "cherry" because the list shifted left.
Hint: RemoveAt shifts list left; index 1 now points to next item [OK]
Common Mistakes:
Assuming removed item still exists
Expecting original index items unchanged
Confusing RemoveAt with Remove
4. Identify the error in this C# code snippet using List<string>:
List<string> colors = new List<string>();
colors.Add("red");
colors[1] = "blue";
Console.WriteLine(colors[1]);
medium
A. IndexOutOfRangeException because index 1 does not exist yet.
B. Syntax error in Add method usage.
C. Cannot assign string to List<string> element.
D. No error; code runs and prints 'blue'.
Solution
Step 1: Analyze list content after Add
After colors.Add("red"), list has one element at index 0 only.
Step 2: Check assignment to colors[1]
colors[1] does not exist yet, so assigning to it causes IndexOutOfRangeException.
Final Answer:
IndexOutOfRangeException because index 1 does not exist yet. -> Option A
Quick Check:
Assigning to non-existing index throws exception [OK]
Hint: List index must exist before assignment; use Add to add items [OK]
Common Mistakes:
Trying to assign to index without adding
Confusing Add and index assignment
Expecting automatic list expansion
5. Given a List<int> named numbers containing {1, 2, 3, 4, 5}, which code snippet correctly doubles each number in the list?
hard
A. numbers = numbers.Select(n => n * 2).ToList();
B. for (int i = 0; i < numbers.Count; i++) { numbers[i] = numbers[i] * 2; }
C. foreach (int n in numbers) { n = n * 2; }
D. numbers.ForEach(n => n = n * 2);
Solution
Step 1: Understand how to modify List elements
Using a for loop with index allows modifying elements directly by assignment.
Step 2: Evaluate each option's effect
for (int i = 0; i < numbers.Count; i++) { numbers[i] = numbers[i] * 2; } modifies elements in place. foreach (int n in numbers) { n = n * 2; } modifies copy of elements (no effect). numbers = numbers.Select(n => n * 2).ToList(); creates a new list but requires LINQ and ToList(). numbers.ForEach(n => n = n * 2); modifies copies in ForEach (no effect).
Final Answer:
for (int i = 0; i < numbers.Count; i++) { numbers[i] = numbers[i] * 2; } -> Option B
Quick Check:
Use for loop with index to update List elements [OK]
Hint: Use for loop with index to update List items directly [OK]
Common Mistakes:
Using foreach expecting to modify list items
Using ForEach with lambda that doesn't assign back