Explain it simply
Collections (List), in plain language
List<T> is a collection whose size can change. Arrays are fixed-size, but with a List you can add, remove, and search for items much more easily. When you don't know ahead of time how many items you'll have, List is the go-to choice in real projects.
Problem → Why → Solution
The problem — why this concept matters
Returning an internal mutable List directly lets callers corrupt class state.
A common mistake
Assuming one successful happy path proves the code is correct, maintainable, and production-ready.
Core concept — understand how it works
`List<T>` is an ordered resizable generic collection with indexed access, add or remove operations, and enumeration. Consider complexity, duplicates, capacity, reference elements, and mutation during enumeration.
- task or collection
- .NET API and pipeline
- typed result or failure
Explain the core concept and runtime behavior of Collections (List)
Core syntax or pattern
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<string> tasks = new List<string>();
tasks.Add("Learn C# basics");
tasks.Add("Practice methods");
tasks.Add("Build a mini project");
tasks.Remove("Practice methods");
foreach (string task in tasks)
{
Console.WriteLine(task);
}
}
}`List<T>` is an ordered resizable generic collection with indexed access, add or remove operations, and enumeration. Consider complexity, duplicates, capacity, reference elements, and mutation during enumeration.
Read the code step by step
- task or collection
using System;List<T> is a collection whose size can change. Arrays are fixed-size, but with a List you can add, remove, and search for items much more easily. When you don't know ahead of time how many items you'll have, List is the go-to choice in real projects.
- .NET API and pipeline
tasks.Add("Practice methods");`List<T>` is an ordered resizable generic collection with indexed access, add or remove operations, and enumeration. Consider complexity, duplicates, capacity, reference elements, and mutation during enumeration.
- typed result or failure
}Exercise Add, Find, Remove, copying, and read-only exposure on a contact list, including empty and duplicate cases.
- task or collection
- .NET API and pipeline
- typed result or failure
Success
Explain the core concept and runtime behavior of Collections (List)
Failure
Returning an internal mutable List directly lets callers corrupt class state.
List<T> is a collection whose size can change. Arrays are fixed-size, but with a List you can add, remove, and search for items much more easily. When you don't know ahead of time how many items you'll have, List is the go-to choice in real projects.
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<string> tasks = new List<string>();
tasks.Add("Learn C# basics");
tasks.Add("Practice methods");
tasks.Add("Build a mini project");
tasks.Remove("Practice methods");
foreach (string task in tasks)
{
Console.WriteLine(task);
}
}
}What you'll learn from this code
using System.Collections.Generic;needs to be added before you can useList<T>.Add()adds an item.Remove()removes an item.foreachloops through every item in the list.
Learn C# basics Build a mini projectReal-world usage
How to apply it in a real project
Exercise Add, Find, Remove, copying, and read-only exposure on a contact list, including empty and duplicate cases.
Mini challenge
Build it yourself
Exercise Add, Find, Remove, copying, and read-only exposure on a contact list, including empty and duplicate cases.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<string> tasks = new List<string>();
tasks.Add("Learn C# basics");
tasks.Add("Practice methods");
tasks.Add("Build a mini project");
tasks.Remove("Practice methods");
foreach (string task in tasks)
{
Console.WriteLine(task);
}
}
}Expected: Explain the core concept and runtime behavior of Collections (List)
Show hint
Exercise Add, Find, Remove, copying, and read-only exposure on a contact list, including empty and duplicate cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- task or collection → .NET API and pipeline → typed result or failure
- `List<T>` is an ordered resizable generic collection with indexed access, add or remove operations, and enumeration. Consider complexity, duplicates, capacity, reference elements, and mutation during enumeration.
- Returning an internal mutable List directly lets callers corrupt class state.
- Exercise Add, Find, Remove, copying, and read-only exposure on a contact list, including empty and duplicate cases.
Collections and data structures — Microsoft Learn