Store, search, and manage collections of data in WinForms
In WinForms apps, you often need to store data that grows or changes β like user entries, search results, or items in memory. Enter List<T>
: a dynamic, flexible collection that lets you add, find, remove, and sort items easily.
Whether you’re working with names, numbers, or custom objects, List<T>
gives you full control without needing arrays or databases.
π§° What Is List<T>
?
- A generic collection that holds items of a specific type (
T
) - Can grow or shrink as needed
- Part of the
System.Collections.Generic
namespace
π§ Add this at the top of your form:
using System.Collections.Generic;
π¦ Declaring and Initializing a List
List<string> names = new List<string>();
List<int> scores = new List<int>();
β You can store strings, numbers, or even custom classes
β Adding Items
names.Add("Alice");
names.Add("Ben");
names.Add("Clara");
You can also populate a list at once:
List<string> colors = new List<string> { "Red", "Blue", "Green" };
π Searching for Items
if (names.Contains("Clara"))
{
MessageBox.Show("Found Clara!");
}
int index = names.IndexOf("Ben");
β
Use .Contains()
, .IndexOf()
, .Find()
, or LINQ for custom search logic
β Removing Items
names.Remove("Alice"); // Removes first match
names.RemoveAt(0); // Removes by index
names.Clear(); // Removes all items
π Example: Add & Show Names in a ListBox
List<string> students = new List<string>();
private void btnAdd_Click(object sender, EventArgs e)
{
students.Add(txtName.Text);
txtName.Clear();
RefreshList();
}
private void RefreshList()
{
listBox1.Items.Clear();
foreach (string student in students)
listBox1.Items.Add(student);
}
β
Keeps your list in sync with the UI
β
You can reuse RefreshList()
after removing items too
π§ Why Use List<T>
Instead of Arrays?
Feature | List<T> | Array |
---|---|---|
Resizable | β Yes | β Fixed size |
Built-in search | β
.Contains() etc. | β Manual loops needed |
Add/Remove | β Easy | β Must copy manually |
π§ͺ Quick Challenge
π§© Build a form that:
- Has a textbox for adding names
- Stores names in a
List<string>
- Displays the list in a
ListBox
- Lets you remove a selected item with a βRemoveβ button
π Summary
Operation | Code Example |
---|---|
Create List | List<string> names = new(); |
Add Item | names.Add("Alice"); |
Search | names.Contains("Ben") |
Remove by Value | names.Remove("Alice") |
Remove by Index | names.RemoveAt(0) |
Clear All | names.Clear(); |
β Best Practices
- β
Use
List<T>
for any in-memory collection - β
Wrap UI updates (like
ListBox.Items
) in a refresh method - β
Always check
.Contains()
before removing items - β
Use
List<T>
with classes for richer data (e.g.,List<Student>
)
π Want to Go Further?
- Learn about
List<T>.Find()
and LINQ for advanced searching - Sort your list with
.Sort()
or custom comparers - Use
List<T>
to back forms, tables, or data-bound controls - Serialize a
List<T>
to a file for storage
π¬ Want help adapting your WinForms UI to use List<T>
?
Show us your form layout β Weβll help you wire up a clean, dynamic list logic.